Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/core/test/events.test.ts
1029 views
1
import { Helpers } from '@secret-agent/testing';
2
import ICoreEventPayload from '@secret-agent/interfaces/ICoreEventPayload';
3
import { ITestKoaServer } from '@secret-agent/testing/helpers';
4
import { LocationStatus } from '@secret-agent/interfaces/Location';
5
import Core, { Session } from '../index';
6
import ConnectionToClient from '../server/ConnectionToClient';
7
8
let koaServer: ITestKoaServer;
9
let connection: ConnectionToClient;
10
const onEventFn = jest.fn();
11
12
beforeAll(async () => {
13
koaServer = await Helpers.runKoaServer();
14
connection = Core.addConnection();
15
Helpers.onClose(() => connection.disconnect(), true);
16
connection.on('message', payload => {
17
if ((payload as ICoreEventPayload).listenerId) {
18
onEventFn(payload);
19
}
20
});
21
});
22
afterAll(async () => {
23
await Helpers.afterAll();
24
});
25
afterEach(Helpers.afterEach);
26
27
describe('Core events tests', () => {
28
it('receives close event when closed', async () => {
29
const meta = await connection.createSession();
30
await connection.addEventListener(meta, null, 'close');
31
await Session.get(meta.sessionId).close();
32
33
expect(onEventFn.mock.calls).toHaveLength(1);
34
});
35
36
it('receives resource events', async () => {
37
onEventFn.mockClear();
38
39
const meta = await connection.createSession();
40
await connection.addEventListener(meta, null, 'resource');
41
42
koaServer.get('/page1', ctx => (ctx.body = '<body><img src="/resource.png"></body>'));
43
koaServer.get('/page2', ctx => (ctx.body = '<body><img src="/resource.png"></body>'));
44
45
const tab = Session.getTab(meta);
46
await tab.goto(`${koaServer.baseUrl}/page1`);
47
await tab.waitForLoad(LocationStatus.PaintingStable);
48
49
await tab.goto(`${koaServer.baseUrl}/page2`);
50
await tab.waitForLoad(LocationStatus.PaintingStable);
51
52
// ToDo: this should really be 2; it's emitting base document as an resource
53
expect(onEventFn.mock.calls).toHaveLength(4);
54
}, 10e3);
55
56
it('removes event listeners', async () => {
57
onEventFn.mockClear();
58
59
const meta = await connection.createSession();
60
const { listenerId } = await connection.addEventListener(meta, null, 'resource');
61
62
koaServer.get('/page1', ctx => (ctx.body = '<body><img src="/resource.png"></body>'));
63
koaServer.get('/page2', ctx => (ctx.body = '<body><img src="/resource.png"></body>'));
64
65
const tab = Session.getTab(meta);
66
await tab.goto(`${koaServer.baseUrl}/page1`);
67
await tab.waitForLoad(LocationStatus.AllContentLoaded);
68
69
await connection.removeEventListener(meta, listenerId);
70
71
await tab.goto(`${koaServer.baseUrl}/page2`);
72
await tab.waitForLoad(LocationStatus.PaintingStable);
73
74
// ToDo: this should really be 1; it's emitting base document as an resource
75
expect(onEventFn.mock.calls).toHaveLength(2);
76
}, 10e3);
77
});
78
79