Files
raucao 4c9d6093c0 Skip the Sockethub server step in calendar setup when already configured
Track whether a Sockethub URL has been stored and start the ICS and CalDAV add flows past the server step for returning users, while keeping it for first-timers. Back navigation stops before the removed step, and the step's helper text now links Sockethub to its website.
2026-11-07 13:38:33 +04:00

205 lines
6.6 KiB
Plaintext

import { module, test } from 'qunit';
import { setupRenderingTest } from 'dashtab/tests/helpers';
import { render, fillIn, click, triggerEvent } from '@ember/test-helpers';
import CalendarCaldavForm from 'dashtab/components/settings/integrations/calendar-caldav-form';
function registerServices(owner, overrides = {}) {
const calendar = overrides.calendar ?? {
fetchCalendars: async () => [],
configureCaldav: async () => {},
};
const sockethub = overrides.sockethub ?? {
url: 'https://sh.example.org',
hasStoredUrl: false,
loadUrl: async () => 'https://sh.example.org',
async setUrl(url) {
this.url = url;
this.hasStoredUrl = true;
},
};
owner.register('service:calendar', calendar, { instantiate: false });
owner.register('service:sockethub', sockethub, { instantiate: false });
}
module(
'Integration | Component | settings/integrations/calendar-caldav-form',
function (hooks) {
setupRenderingTest(hooks);
test('walks through server, account and calendar selection', async function (assert) {
const fetched = [];
const configured = [];
let closed = false;
registerServices(this.owner, {
calendar: {
fetchCalendars: async (config) => {
fetched.push(config);
return [
{ id: 'c1', name: 'Work', components: ['event'] },
{ id: 'c2', name: 'Tasks', components: ['task'] },
];
},
configureCaldav: async (config, key) =>
configured.push({ config, key }),
},
});
const closeDialog = () => {
closed = true;
};
await render(
<template><CalendarCaldavForm @close={{closeDialog}} /></template>
);
assert.dom().containsText('Add CalDAV Calendar');
assert.dom('#caldav-server-url').hasValue('https://sh.example.org');
assert.dom('a[href="https://sockethub.org"]').exists();
await triggerEvent('form', 'submit');
assert.dom('#caldav-url').exists();
assert.dom('[data-test-back]').exists();
await fillIn('#caldav-url', 'cal.example/dav/');
await fillIn('#caldav-username', 'alice');
await fillIn('#caldav-password', 'secret');
await triggerEvent('form', 'submit');
assert.strictEqual(fetched.length, 1);
assert.strictEqual(fetched[0].url, 'https://cal.example/dav/');
assert.dom('#caldav-calendar-0').exists();
// A tasks-only calendar is not offered.
assert.dom('#caldav-calendar-1').doesNotExist();
await click('#caldav-calendar-0');
await triggerEvent('form', 'submit');
assert.strictEqual(configured.length, 1);
assert.deepEqual(configured[0].config.selectedCalendarIds, ['c1']);
assert.strictEqual(configured[0].config.calendars.length, 2);
assert.strictEqual(configured[0].key, undefined);
assert.true(closed);
});
test('validates the CalDAV URL', async function (assert) {
const fetched = [];
registerServices(this.owner, {
calendar: {
fetchCalendars: async (...args) => fetched.push(args),
configureCaldav: async () => {},
},
});
const noop = () => {};
await render(<template><CalendarCaldavForm @close={{noop}} /></template>);
await triggerEvent('form', 'submit');
await fillIn('#caldav-username', 'alice');
await fillIn('#caldav-password', 'secret');
await triggerEvent('form', 'submit');
assert.dom().containsText('Please enter a valid CalDAV URL');
assert.strictEqual(fetched.length, 0);
});
test('requires at least one calendar', async function (assert) {
const configured = [];
registerServices(this.owner, {
calendar: {
fetchCalendars: async () => [
{ id: 'c1', name: 'Work', components: ['event'] },
],
configureCaldav: async (...args) => configured.push(args),
},
});
const noop = () => {};
await render(<template><CalendarCaldavForm @close={{noop}} /></template>);
await triggerEvent('form', 'submit');
await fillIn('#caldav-url', 'https://cal.example/dav/');
await fillIn('#caldav-username', 'alice');
await fillIn('#caldav-password', 'secret');
await triggerEvent('form', 'submit');
await triggerEvent('form', 'submit');
assert.dom().containsText('Select at least one calendar');
assert.strictEqual(configured.length, 0);
});
test('skips the server step when a Sockethub URL is stored', async function (assert) {
registerServices(this.owner, {
sockethub: {
url: 'https://sh.example.org',
hasStoredUrl: true,
loadUrl: async () => 'https://sh.example.org',
setUrl: async () => {},
},
calendar: {
fetchCalendars: async () => [
{ id: 'c1', name: 'Work', components: ['event'] },
],
configureCaldav: async () => {},
},
});
const noop = () => {};
await render(<template><CalendarCaldavForm @close={{noop}} /></template>);
assert.dom('#caldav-server-url').doesNotExist();
assert.dom('#caldav-url').exists();
assert.dom('[data-test-back]').doesNotExist();
await fillIn('#caldav-url', 'https://cal.example/dav/');
await fillIn('#caldav-username', 'alice');
await fillIn('#caldav-password', 'secret');
await triggerEvent('form', 'submit');
assert.dom('#caldav-calendar-0').exists();
assert.dom('[data-test-back]').exists();
});
test('edit opens on the selection step with the stored calendars', async function (assert) {
const fetched = [];
registerServices(this.owner, {
calendar: {
fetchCalendars: async (config) => {
fetched.push(config);
return [{ id: 'c1', name: 'Work', components: ['event'] }];
},
configureCaldav: async () => {},
},
});
this.props = {
mode: 'edit',
integration: {
id: 'calendar-caldav-cal.example.com-alice',
url: 'https://cal.example/dav/',
username: 'alice',
password: 'secret',
calendars: [{ id: 'c1', name: 'Work', components: ['event'] }],
selectedCalendarIds: ['c1'],
},
};
const noop = () => {};
await render(
<template>
<CalendarCaldavForm @props={{this.props}} @close={{noop}} />
</template>
);
assert.dom().containsText('Edit CalDAV Calendar');
assert.dom('#caldav-calendar-0').isChecked();
assert.strictEqual(fetched.length, 1);
assert.strictEqual(fetched[0].password, 'secret');
});
}
);