Skip to content

Commit b21e882

Browse files
committed
bootstrapping poll test
1 parent 6afcd61 commit b21e882

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* This file is part of the Symfony package.
3+
*
4+
* (c) Fabien Potencier <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
'use strict';
11+
12+
import { shutdownTest, createTest, initComponent } from '../tools';
13+
import { createEvent, fireEvent, getByText, waitFor } from '@testing-library/dom';
14+
import userEvent from '@testing-library/user-event';
15+
import fetchMock from 'fetch-mock-jest';
16+
17+
describe('LiveController polling Tests', () => {
18+
afterEach(() => {
19+
shutdownTest();
20+
})
21+
22+
it('starts a poll', async () => {
23+
const test = await createTest({ renderCount: 0 }, (data: any) => `
24+
<div ${initComponent(data)} data-poll>
25+
<span>Render count: ${data.renderCount}</span>
26+
</div>
27+
`);
28+
29+
// poll 1
30+
test.expectsAjaxCall('get')
31+
.expectSentData(test.initialData)
32+
.serverWillChangeData((data: any) => {
33+
data.renderCount = 1;
34+
})
35+
.init();
36+
// poll 2
37+
test.expectsAjaxCall('get')
38+
.expectSentData({renderCount: 1})
39+
.serverWillChangeData((data: any) => {
40+
data.renderCount = 2;
41+
})
42+
.init();
43+
44+
await waitFor(() => expect(test.element).toHaveTextContent('Render count: 1'), {
45+
timeout: 2100
46+
});
47+
await waitFor(() => expect(test.element).toHaveTextContent('Render count: 2'), {
48+
timeout: 2100
49+
});
50+
});
51+
52+
it('is controllable via modifiers', async () => {
53+
const test = await createTest({ renderCount: 0 }, (data: any) => `
54+
<div ${initComponent(data)} data-poll="delay(500)|$render">
55+
<span>Render count: ${data.renderCount}</span>
56+
</div>
57+
`);
58+
59+
// poll 1
60+
test.expectsAjaxCall('get')
61+
.expectSentData(test.initialData)
62+
.serverWillChangeData((data: any) => {
63+
data.renderCount = 1;
64+
})
65+
.init();
66+
// poll 2
67+
test.expectsAjaxCall('get')
68+
.expectSentData({renderCount: 1})
69+
.serverWillChangeData((data: any) => {
70+
data.renderCount = 2;
71+
})
72+
.init();
73+
74+
// only wait for about 500ms this time
75+
await waitFor(() => expect(test.element).toHaveTextContent('Render count: 1'), {
76+
timeout: 600
77+
});
78+
await waitFor(() => expect(test.element).toHaveTextContent('Render count: 2'), {
79+
timeout: 600
80+
});
81+
});
82+
83+
it('can also call a live action', async () => {
84+
const test = await createTest({ renderCount: 0 }, (data: any) => `
85+
<div ${initComponent(data)} data-poll="delay(500)|saveAction">
86+
<span>Render count: ${data.renderCount}</span>
87+
</div>
88+
`);
89+
90+
// poll 1
91+
test.expectsAjaxCall('post')
92+
.expectSentData(test.initialData)
93+
.expectActionCalled('saveAction')
94+
.serverWillChangeData((data: any) => {
95+
data.renderCount = 1;
96+
})
97+
.init();
98+
// poll 2
99+
test.expectsAjaxCall('post')
100+
.expectSentData({renderCount: 1})
101+
.expectActionCalled('saveAction')
102+
.serverWillChangeData((data: any) => {
103+
data.renderCount = 2;
104+
})
105+
.init();
106+
107+
// only wait for about 500ms this time
108+
await waitFor(() => expect(test.element).toHaveTextContent('Render count: 1'), {
109+
timeout: 600
110+
});
111+
await waitFor(() => expect(test.element).toHaveTextContent('Render count: 2'), {
112+
timeout: 600
113+
});
114+
});
115+
});

0 commit comments

Comments
 (0)