Skip to content

Commit ad43ba9

Browse files
committed
Add tests for user-provided LiveObjects types to the LiveObjects package test
1 parent ad2d457 commit ad43ba9

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { LiveCounter, LiveMap } from 'ably';
2+
3+
declare global {
4+
export interface LiveObjectsTypes {
5+
root: {
6+
numberKey: number;
7+
stringKey: string;
8+
booleanKey: boolean;
9+
couldBeUndefined?: string;
10+
mapKey?: LiveMap<{
11+
foo: 'bar';
12+
nestedMap?: LiveMap<{
13+
baz: 'qux';
14+
}>;
15+
}>;
16+
counterKey?: LiveCounter;
17+
};
18+
}
19+
}

test/package/browser/template/src/index-liveobjects.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import * as Ably from 'ably';
22
import LiveObjects from 'ably/liveobjects';
33
import { createSandboxAblyAPIKey } from './sandbox';
44

5+
// TODO: check we can refer to types exported by LiveObjects plugin.
6+
57
globalThis.testAblyPackage = async function () {
68
const key = await createSandboxAblyAPIKey({ featureFlags: ['enableChannelState'] });
79

@@ -13,9 +15,23 @@ globalThis.testAblyPackage = async function () {
1315
const root = await liveObjects.getRoot();
1416

1517
// check root is recognized as LiveMap TypeScript type
16-
root.get('someKey');
1718
root.size();
1819

20+
// check custom user provided typings from ably.config.d.ts are working:
21+
// keys on a root:
22+
const aNumber: number = root.get('numberKey');
23+
const aString: string = root.get('stringKey');
24+
const aBoolean: boolean = root.get('booleanKey');
25+
const couldBeUndefined: string | undefined = root.get('couldBeUndefined');
26+
// live objects on a root:
27+
const counter: Ably.LiveCounter | undefined = root.get('counterKey');
28+
const map: LiveObjectsTypes['root']['mapKey'] = root.get('mapKey');
29+
// check string literal types works
30+
// need to use nullish coalescing as we didn't actually create any data on the root,
31+
// so the next calls would fail. we only need to check that TypeScript types work
32+
const foo: 'bar' = map?.get('foo')!;
33+
const baz: 'qux' = map?.get('nestedMap')?.get('baz')!;
34+
1935
// check LiveMap subscription callback has correct TypeScript types
2036
const { unsubscribe } = root.subscribe(({ update }) => {
2137
switch (update.someKey) {
@@ -29,10 +45,8 @@ globalThis.testAblyPackage = async function () {
2945
});
3046
unsubscribe();
3147

32-
// check LiveCounter types also behave as expected
33-
const counter = root.get('randomKey') as Ably.LiveCounter | undefined;
34-
// use nullish coalescing as we didn't actually create a counter object on the root,
35-
// so the next calls would fail. we only need to check that TypeScript types work
48+
// check LiveCounter type also behaves as expected
49+
// same deal with nullish coalescing
3650
counter?.value();
3751
const counterSubscribeResponse = counter?.subscribe(({ update }) => {
3852
const shouldBeANumber: number = update.inc;

test/package/browser/template/src/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"include": ["**/*.ts", "**/*.tsx"],
33
"compilerOptions": {
4+
"strictNullChecks": true,
45
"resolveJsonModule": true,
56
"esModuleInterop": true,
67
"module": "esnext",

0 commit comments

Comments
 (0)