-
Notifications
You must be signed in to change notification settings - Fork 7
/
temba-list.test.ts
95 lines (77 loc) · 2.5 KB
/
temba-list.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { assert, expect } from '@open-wc/testing';
import * as sinon from 'sinon';
import { useFakeTimers } from 'sinon';
import { CustomEventType } from '../src/interfaces';
import { TembaList } from '../src/list/TembaList';
import { assertScreenshot, getClip, getComponent } from './utils.test';
let clock: any;
const TAG = 'temba-list';
const getList = async (attrs: any = {}) => {
const list = (await getComponent(TAG, attrs)) as TembaList;
if (!list.endpoint) {
return list;
}
return new Promise<TembaList>((resolve) => {
list.addEventListener(
CustomEventType.FetchComplete,
async () => {
resolve(list);
},
{ once: true }
);
});
};
describe('temba-list', () => {
beforeEach(function () {
clock = useFakeTimers();
});
afterEach(function () {
clock.restore();
});
it('can be created', async () => {
const list: TembaList = await getList();
assert.instanceOf(list, TembaList);
expect(list.items.length).to.equal(0);
});
it('shows empty list with bad endpoint', async () => {
const list: TembaList = await getList({ endpoint: 'expected-404.json' });
expect(list.items.length).to.equal(0);
});
it('renders with endpoint', async () => {
const list: TembaList = await getList({
endpoint: '/test-assets/list/temba-list.json'
});
expect(list.items.length).to.equal(4);
await assertScreenshot('list/items', getClip(list));
});
it('fires change event on cursor change', async () => {
const list: TembaList = await getList({
endpoint: '/test-assets/list/temba-list.json'
});
const changeTest = new Promise<void>((resolve) => {
list.addEventListener('change', () => {
resolve();
});
});
list.cursorIndex = 1;
await changeTest;
await assertScreenshot('list/items-selected', getClip(list));
});
it('fires change when first element changes after fetch', async () => {
const list: TembaList = await getList({
endpoint: '/test-assets/list/temba-list.json'
});
// spy on change event
const changeEvent = sinon.spy();
list.addEventListener('change', changeEvent);
const refreshTest = new Promise<void>((resolve) => {
list.addEventListener(CustomEventType.FetchComplete, () => {
resolve();
});
});
list.endpoint = '/test-assets/list/temba-list-shorter.json';
await refreshTest;
assert(changeEvent.called, 'change event not fired');
await assertScreenshot('list/items-updated', getClip(list));
});
});