-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinteractor.test.ts
118 lines (98 loc) · 3.31 KB
/
interactor.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { describe, it } from 'node:test';
import expect from 'expect';
import { dom } from './helpers';
import { Header, Link, MainNav } from './fixtures';
describe('Interactor', () => {
describe('instantiation', () => {
describe('no arguments', () => {
it('just uses the selector to locate', async () => {
dom(`
<nav id="main-nav"></nav>
`);
await expect(MainNav().exists()).resolves.toBeUndefined();
});
});
});
describe('.perform', () => {
it('can use action to interact with element', async () => {
dom(`
<a id="foo" href="/foobar">Foo Bar</a>
<div id="target"></div>
<script>
foo.onclick = () => {
target.innerHTML = '<h1>Hello!</h1>';
}
</script>
`);
await Link('Foo Bar').perform((e) => e.click());
await Header('Hello!').exists();
});
it('does nothing unless awaited', async () => {
dom(`
<a id="foo" href="/foobar">Foo Bar</a>
<div id="target"></div>
<script>
foo.onclick = () => {
target.innerHTML = '<h1>Hello!</h1>';
}
</script>
`);
Link('Foo Bar').perform((e) => e.click());
await Header('Hello!').absent();
});
it('can return description of interaction', () => {
expect(Link('Foo Bar').perform((e) => e.click()).description).toEqual('run perform on link "Foo Bar"');
});
it('throws an error if ambiguous', async () => {
dom(`
<p><a href="/foo">Foo</a></p>
<p><a href="/bar"">Foo</a></p>
`);
await expect(Link('Foo').perform((e) => e.click())).rejects.toHaveProperty('message', [
'link "Foo" matches multiple elements:', '',
'- <a href="/foo">',
'- <a href="/bar"">',
].join('\n'))
});
it('returns a value outside', async () => {
dom(`
<a data-foo="bar" href="/foobar">Foo</a>
`);
await expect(Link('Foo').perform((e) => e.getAttribute('data-foo'))).resolves.toEqual('bar')
})
});
describe('.assert', () => {
it('can assert on state', async () => {
dom(`
<a data-foo="foo" href="/foobar">Foo Bar</a>
`);
await Link('Foo Bar').assert((e) => expect(e.dataset.foo).toEqual('foo'));
});
it('is rejected if assertion fails', async () => {
dom(`
<a data-foo="foo" href="/foobar">Foo Bar</a>
`);
await expect(Link('Foo Bar').assert((e) => expect(e.dataset.foo).toEqual('incorrect'))).rejects;
});
it('can return description of interaction', () => {
expect(Link('Foo Bar').assert(() => true).description).toEqual('link "Foo Bar" asserts');
});
it('throws an error if ambiguous', async () => {
dom(`
<p><a href="/foo">Foo</a></p>
<p><a href="/bar"">Foo</a></p>
`);
await expect(Link('Foo').assert(() => true)).rejects.toHaveProperty('message', [
'link "Foo" matches multiple elements:', '',
'- <a href="/foo">',
'- <a href="/bar"">',
].join('\n'))
});
it('can be used normally if runner is currently in assertion state', async () => {
dom(`
<a data-foo="foo" href="/foobar">Foo Bar</a>
`);
await Link('Foo Bar').assert((e) => expect(e.dataset.foo).toEqual('foo'));
});
});
});