-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSpaces.test.ts
52 lines (41 loc) · 1.67 KB
/
Spaces.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
import { it, describe, expect, expectTypeOf, vi, beforeEach } from 'vitest';
import { Realtime, Types } from 'ably/promises';
import Spaces, { type ClientWithOptions } from './Spaces.js';
interface SpacesTestContext {
client: ClientWithOptions;
}
vi.mock('ably/promises');
describe('Spaces', () => {
beforeEach<SpacesTestContext>((context) => {
context.client = new Realtime({ key: 'asd' }) as ClientWithOptions;
});
it<SpacesTestContext>('expects the injected client to be of the type RealtimePromise', ({ client }) => {
const spaces = new Spaces(client);
expectTypeOf(spaces.client).toMatchTypeOf<Types.RealtimePromise>();
});
it<SpacesTestContext>('creates and retrieves spaces successfully', async ({ client }) => {
const channels = client.channels;
const spy = vi.spyOn(channels, 'get');
const spaces = new Spaces(client);
await spaces.get('test');
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith('test-space');
});
it<SpacesTestContext>('applies the agent header to an existing SDK instance', ({ client }) => {
const spaces = new Spaces(client);
expect(client.options.agents).toEqual({
'ably-spaces': spaces.version,
'space-custom-client': true,
});
});
it<SpacesTestContext>('extend the agents array when it already exists', ({ client }) => {
(client as ClientWithOptions).options.agents = { 'some-client': '1.2.3' };
const spaces = new Spaces(client);
const ablyClient = spaces.client as ClientWithOptions;
expect(ablyClient.options.agents).toEqual({
'some-client': '1.2.3',
'ably-spaces': spaces.version,
'space-custom-client': true,
});
});
});