Files
raucao 303b26793f Reorder the custom link option and its icon controls
Sort the generic "Custom Link" entry last in the add-integration dialog so
it reads as an "add anything" action after the concrete integrations, and
move the icon controls below the URL and name fields in the custom link
form so the form flows from link details to its optional icon.
2026-11-13 21:09:21 +04:00

296 lines
8.9 KiB
Plaintext

import { module, test } from 'qunit';
import { setupRenderingTest } from 'dashtab/tests/helpers';
import { render, fillIn, triggerEvent, click } from '@ember/test-helpers';
import CustomLinkForm from 'dashtab/components/settings/integrations/custom-link-form';
const noop = () => {};
// Real 1x1 images so the <img> actually loads (an invalid data URL fires
// `error` and the component falls back to the initial).
const PNG_ICON =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==';
const GIF_ICON =
'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
function registerServices(owner, overrides = {}) {
const storage = overrides.storage ?? { set: async () => {} };
const sites = overrides.sites ?? { reload: async () => {} };
const siteIcons = overrides.siteIcons ?? {
iconFor: async () => undefined,
iconFromFile: async () => ({ dataUrl: PNG_ICON, maskable: false }),
};
owner.register('service:storage', storage, { instantiate: false });
owner.register('service:sites', sites, { instantiate: false });
owner.register('service:site-icons', siteIcons, { instantiate: false });
}
function selectFile(element, file) {
const input = element.querySelector('[data-test-icon-upload]');
const dataTransfer = new DataTransfer();
dataTransfer.items.add(file);
input.files = dataTransfer.files;
return triggerEvent(input, 'change');
}
module(
'Integration | Component | settings/integrations/custom-link-form',
function (hooks) {
setupRenderingTest(hooks);
test('it renders the add form', async function (assert) {
registerServices(this.owner);
await render(<template><CustomLinkForm @close={{noop}} /></template>);
assert.dom().containsText('Add Custom Link');
assert.dom('#custom-link-url').exists().hasValue('');
assert.dom('#custom-link-name').exists();
assert.dom('[data-test-icon-upload]').exists();
});
test('it places the icon controls after the URL and name fields', async function (assert) {
registerServices(this.owner);
await render(<template><CustomLinkForm @close={{noop}} /></template>);
const name = this.element.querySelector('#custom-link-name');
const upload = this.element.querySelector('[data-test-icon-upload]');
assert.true(
Boolean(
name.compareDocumentPosition(upload) &
Node.DOCUMENT_POSITION_FOLLOWING
),
'the icon controls come after the name field'
);
});
test('it rejects an empty or invalid URL without storing anything', async function (assert) {
const saved = [];
registerServices(this.owner, {
storage: { set: async (...args) => saved.push(args) },
});
await render(<template><CustomLinkForm @close={{noop}} /></template>);
await triggerEvent('form', 'submit');
assert
.dom('[data-test-error]')
.hasText('Please enter a valid website URL');
await fillIn('#custom-link-url', 'javascript:alert(1)');
await triggerEvent('form', 'submit');
assert
.dom('[data-test-error]')
.hasText('Please enter a valid website URL');
assert.strictEqual(saved.length, 0);
});
test('it stores a normalized record and reloads the sites', async function (assert) {
const saved = [];
let reloaded = 0;
let closed = false;
registerServices(this.owner, {
storage: {
set: async (namespace, key, value) =>
saved.push({ namespace, key, value }),
},
sites: {
reload: async () => {
reloaded++;
},
},
});
const closeDialog = () => {
closed = true;
};
await render(
<template><CustomLinkForm @close={{closeDialog}} /></template>
);
await fillIn('#custom-link-url', 'example.com/path');
await fillIn('#custom-link-name', 'Example');
await triggerEvent('form', 'submit');
assert.strictEqual(saved.length, 1);
assert.strictEqual(saved[0].namespace, 'integrations');
assert.true(saved[0].key.startsWith('custom-'));
assert.strictEqual(saved[0].value.type, 'custom');
assert.strictEqual(saved[0].value.status, 'active');
assert.strictEqual(saved[0].value.url, 'https://example.com/path');
assert.strictEqual(saved[0].value.name, 'Example');
assert.strictEqual(reloaded, 1);
assert.true(closed);
});
test('edit mode prefills and keeps the key, name and custom icon', async function (assert) {
const saved = [];
const customIcon = {
dataUrl: PNG_ICON,
maskable: false,
};
registerServices(this.owner, {
storage: {
set: async (namespace, key, value) =>
saved.push({ namespace, key, value }),
},
});
this.props = {
mode: 'edit',
integration: {
id: 'custom-1',
url: 'https://example.com/',
name: 'Example',
customIcon,
icon: customIcon,
},
};
await render(
<template>
<CustomLinkForm @props={{this.props}} @close={{noop}} />
</template>
);
assert.dom().containsText('Edit Link');
assert.dom('#custom-link-url').hasValue('https://example.com/');
assert.dom('#custom-link-name').hasValue('Example');
assert
.dom('[data-test-preview-image]')
.hasAttribute('src', customIcon.dataUrl);
await triggerEvent('form', 'submit');
assert.strictEqual(saved.length, 1);
assert.strictEqual(saved[0].key, 'custom-1');
assert.deepEqual(saved[0].value.customIcon, customIcon);
});
test('previews the discovered icon on URL blur', async function (assert) {
registerServices(this.owner, {
siteIcons: {
iconFor: async () => ({
dataUrl: GIF_ICON,
maskable: true,
}),
iconFromFile: async () => undefined,
},
});
await render(<template><CustomLinkForm @close={{noop}} /></template>);
await fillIn('#custom-link-url', 'example.com');
await triggerEvent('#custom-link-url', 'blur');
assert.dom('[data-test-preview-image]').hasAttribute('src', GIF_ICON);
});
test('uploading an icon shows it and stores it', async function (assert) {
const saved = [];
const uploaded = {
dataUrl: PNG_ICON,
maskable: false,
};
registerServices(this.owner, {
storage: {
set: async (namespace, key, value) =>
saved.push({ namespace, key, value }),
},
siteIcons: {
iconFor: async () => undefined,
iconFromFile: async () => uploaded,
},
});
await render(<template><CustomLinkForm @close={{noop}} /></template>);
await fillIn('#custom-link-url', 'example.com');
await selectFile(
this.element,
new File(['x'], 'icon.png', { type: 'image/png' })
);
assert
.dom('[data-test-preview-image]')
.hasAttribute('src', uploaded.dataUrl);
assert.dom('[data-test-use-site-icon]').exists();
await triggerEvent('form', 'submit');
assert.deepEqual(saved[0].value.customIcon, uploaded);
});
test('it surfaces an upload error', async function (assert) {
registerServices(this.owner, {
siteIcons: {
iconFor: async () => undefined,
iconFromFile: async () => {
throw new Error('Please choose an image file');
},
},
});
await render(<template><CustomLinkForm @close={{noop}} /></template>);
await selectFile(
this.element,
new File(['x'], 'notes.txt', { type: 'text/plain' })
);
assert.dom('[data-test-error]').hasText('Please choose an image file');
});
test('"Use site\'s icon" clears a pinned icon', async function (assert) {
const saved = [];
const customIcon = {
dataUrl: PNG_ICON,
maskable: false,
};
registerServices(this.owner, {
storage: {
set: async (namespace, key, value) =>
saved.push({ namespace, key, value }),
},
siteIcons: {
iconFor: async () => ({
dataUrl: GIF_ICON,
maskable: true,
}),
iconFromFile: async () => undefined,
},
});
this.props = {
mode: 'edit',
integration: {
id: 'custom-1',
url: 'https://example.com/',
customIcon,
icon: customIcon,
},
};
await render(
<template>
<CustomLinkForm @props={{this.props}} @close={{noop}} />
</template>
);
await click('[data-test-use-site-icon]');
assert.dom('[data-test-preview-image]').hasAttribute('src', GIF_ICON);
await triggerEvent('form', 'submit');
assert.strictEqual(saved[0].value.customIcon, undefined);
});
}
);