2019-10-10 02:09:25 +00:00
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupRenderingTest } from 'ember-qunit';
|
|
|
|
import { render, triggerEvent, waitUntil } from '@ember/test-helpers';
|
|
|
|
import hbs from 'htmlbars-inline-precompile';
|
|
|
|
import sinon from 'sinon';
|
|
|
|
|
|
|
|
let file;
|
|
|
|
const data = JSON.stringify({ some: 'content' }, null, 2);
|
|
|
|
const fileEvent = () => {
|
|
|
|
file = new Blob([data], { type: 'application/json' });
|
|
|
|
file.name = 'file.json';
|
|
|
|
return ['change', { files: [file] }];
|
|
|
|
};
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
module('Integration | Component | pgp list', function (hooks) {
|
2019-10-10 02:09:25 +00:00
|
|
|
setupRenderingTest(hooks);
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it renders', async function (assert) {
|
2019-10-10 02:09:25 +00:00
|
|
|
this.set('listLength', 0);
|
2022-10-18 15:46:02 +00:00
|
|
|
await render(hbs`<PgpList @listLength={{this.listLength}} />`);
|
2019-10-10 02:09:25 +00:00
|
|
|
assert.dom('[data-test-empty-text]').exists('shows the empty state');
|
|
|
|
this.set('listLength', 1);
|
|
|
|
assert
|
|
|
|
.dom('[data-test-pgp-file]')
|
|
|
|
.exists({ count: 1 }, 'renders pgp-file one component when length is updated');
|
|
|
|
this.set('listLength', 2);
|
|
|
|
assert
|
|
|
|
.dom('[data-test-pgp-file]')
|
|
|
|
.exists({ count: 2 }, 'renders multiple pgp-file components when length is updated');
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('onDataUpdate is called properly', async function (assert) {
|
2019-10-10 02:09:25 +00:00
|
|
|
this.set('spy', sinon.spy());
|
2022-11-09 23:15:31 +00:00
|
|
|
const event = fileEvent();
|
2019-10-10 02:09:25 +00:00
|
|
|
|
|
|
|
await render(hbs`<PgpList @listLength={{1}} @onDataUpdate={{this.spy}} />`);
|
|
|
|
triggerEvent('[data-test-pgp-file-input]', ...event);
|
|
|
|
|
|
|
|
// FileReader is async, but then we need extra run loop wait to re-render
|
|
|
|
await waitUntil(() => {
|
|
|
|
return !!this.spy.calledOnce;
|
|
|
|
});
|
2022-11-09 23:15:31 +00:00
|
|
|
const expected = [btoa(data)];
|
2019-10-10 02:09:25 +00:00
|
|
|
assert.deepEqual(
|
|
|
|
this.spy.getCall(0).args[0],
|
|
|
|
expected,
|
|
|
|
'calls onchange with an array of base64 converted files'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('sparse filling of multiple files, then shortening', async function (assert) {
|
2019-10-10 02:09:25 +00:00
|
|
|
this.set('spy', sinon.spy());
|
|
|
|
this.set('listLength', 3);
|
|
|
|
|
|
|
|
await render(hbs`<PgpList @listLength={{this.listLength}} @onDataUpdate={{this.spy}} />`);
|
|
|
|
|
|
|
|
// add a file to the first input
|
|
|
|
triggerEvent('[data-test-pgp-file]:nth-child(1) [data-test-pgp-file-input]', ...fileEvent());
|
|
|
|
await waitUntil(() => {
|
|
|
|
return !!this.spy.calledOnce;
|
|
|
|
});
|
2022-11-09 23:15:31 +00:00
|
|
|
const expected = [btoa(data), '', ''];
|
2019-10-10 02:09:25 +00:00
|
|
|
assert.deepEqual(
|
|
|
|
this.spy.getCall(0).args[0],
|
|
|
|
expected,
|
|
|
|
'calls onchange with an array of base64 converted files'
|
|
|
|
);
|
|
|
|
|
|
|
|
// add a file to the third input
|
|
|
|
triggerEvent('[data-test-pgp-file]:nth-child(3) [data-test-pgp-file-input]', ...fileEvent());
|
|
|
|
await waitUntil(() => {
|
|
|
|
return !!this.spy.calledTwice;
|
|
|
|
});
|
2022-11-09 23:15:31 +00:00
|
|
|
const expected2 = [btoa(data), '', btoa(data)];
|
2019-10-10 02:09:25 +00:00
|
|
|
assert.deepEqual(
|
|
|
|
this.spy.getCall(1).args[0],
|
|
|
|
expected2,
|
|
|
|
'calls onchange with an array 2 base64 converted files'
|
|
|
|
);
|
|
|
|
|
|
|
|
// this will trim off the last input which was filled, so we should only have one file in the array
|
|
|
|
// now
|
|
|
|
this.set('listLength', 2);
|
2022-11-09 23:15:31 +00:00
|
|
|
const expected3 = [btoa(data), ''];
|
2019-10-10 02:09:25 +00:00
|
|
|
assert.deepEqual(
|
|
|
|
this.spy.getCall(2).args[0],
|
|
|
|
expected3,
|
|
|
|
'shortens the list with an array with one base64 converted files'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('sparse filling of multiple files, then lengthening', async function (assert) {
|
2019-10-10 02:09:25 +00:00
|
|
|
this.set('spy', sinon.spy());
|
|
|
|
this.set('listLength', 3);
|
|
|
|
|
|
|
|
await render(hbs`<PgpList @listLength={{this.listLength}} @onDataUpdate={{this.spy}} />`);
|
|
|
|
|
|
|
|
// add a file to the first input
|
|
|
|
triggerEvent('[data-test-pgp-file]:nth-child(1) [data-test-pgp-file-input]', ...fileEvent());
|
|
|
|
await waitUntil(() => {
|
|
|
|
return !!this.spy.calledOnce;
|
|
|
|
});
|
2022-11-09 23:15:31 +00:00
|
|
|
const expected = [btoa(data), '', ''];
|
2019-10-10 02:09:25 +00:00
|
|
|
assert.deepEqual(
|
|
|
|
this.spy.getCall(0).args[0],
|
|
|
|
expected,
|
|
|
|
'calls onchange with an array of base64 converted files'
|
|
|
|
);
|
|
|
|
|
|
|
|
// add a file to the third input
|
|
|
|
triggerEvent('[data-test-pgp-file]:nth-child(3) [data-test-pgp-file-input]', ...fileEvent());
|
|
|
|
await waitUntil(() => {
|
|
|
|
return !!this.spy.calledTwice;
|
|
|
|
});
|
2022-11-09 23:15:31 +00:00
|
|
|
const expected2 = [btoa(data), '', btoa(data)];
|
2019-10-10 02:09:25 +00:00
|
|
|
assert.deepEqual(
|
|
|
|
this.spy.getCall(1).args[0],
|
|
|
|
expected2,
|
|
|
|
'calls onchange with an array 2 base64 converted files'
|
|
|
|
);
|
|
|
|
|
|
|
|
// this will add a couple of inputs but should keep the existing 3
|
|
|
|
this.set('listLength', 5);
|
2022-11-09 23:15:31 +00:00
|
|
|
const expected3 = [btoa(data), '', btoa(data), '', ''];
|
2019-10-10 02:09:25 +00:00
|
|
|
assert.deepEqual(
|
|
|
|
this.spy.getCall(2).args[0],
|
|
|
|
expected3,
|
|
|
|
'lengthening the list with an array with one base64 converted files'
|
|
|
|
);
|
|
|
|
});
|
2021-11-04 20:25:15 +00:00
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it should render correct amount of file components on listLength change', async function (assert) {
|
2021-11-04 20:25:15 +00:00
|
|
|
assert.expect(4);
|
|
|
|
|
|
|
|
this.set('listLength', null);
|
|
|
|
|
|
|
|
await render(hbs`
|
|
|
|
<PgpList
|
|
|
|
@listLength={{this.listLength}}
|
|
|
|
/>
|
|
|
|
`);
|
2021-12-17 03:44:29 +00:00
|
|
|
[1, 5, 3, 0].forEach((count) => {
|
2021-11-04 20:25:15 +00:00
|
|
|
this.set('listLength', count);
|
|
|
|
if (count) {
|
|
|
|
assert
|
|
|
|
.dom('[data-test-pgp-file]')
|
|
|
|
.exists({ count }, `Correct number of file inputs render when listLength is updated to ${count}`);
|
|
|
|
} else {
|
|
|
|
assert.dom('[data-test-empty-text]').exists('Placeholder renders when list length is zero');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2019-10-10 02:09:25 +00:00
|
|
|
});
|