Skip to content

Commit

Permalink
Add tests for user-provided LiveObjects types to the LiveObjects pack…
Browse files Browse the repository at this point in the history
…age test
  • Loading branch information
VeskeR committed Nov 28, 2024
1 parent 9cffcbb commit a1e29ff
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
21 changes: 21 additions & 0 deletions test/package/browser/template/src/ably.config.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { LiveCounter, LiveMap } from 'ably';

type CustomRoot = {
numberKey: number;
stringKey: string;
booleanKey: boolean;
couldBeUndefined?: string;
mapKey?: LiveMap<{
foo: 'bar';
nestedMap?: LiveMap<{
baz: 'qux';
}>;
}>;
counterKey?: LiveCounter;
};

declare global {
export interface LiveObjectsTypes {
root: CustomRoot;
}
}
42 changes: 32 additions & 10 deletions test/package/browser/template/src/index-liveobjects.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import * as Ably from 'ably';
import LiveObjects from 'ably/liveobjects';
import { CustomRoot } from './ably.config';
import { createSandboxAblyAPIKey } from './sandbox';

type ExplicitRootType = {
someOtherKey: string;
};

globalThis.testAblyPackage = async function () {
const key = await createSandboxAblyAPIKey({ featureFlags: ['enableChannelState'] });

Expand All @@ -11,12 +16,27 @@ globalThis.testAblyPackage = async function () {
// check liveObjects can be accessed
const liveObjects = channel.liveObjects;
await channel.attach();
// root should be a LiveMap object
const root: Ably.LiveMap = await liveObjects.getRoot();
// expect root to be a LiveMap instance with LiveObjects types defined via the global LiveObjectsTypes interface
// also checks that we can refer to the LiveObjects types exported from 'ably' by referencing a LiveMap interface
const root: Ably.LiveMap<CustomRoot> = await liveObjects.getRoot();

// check root has expected LiveMap TypeScript type methods
const size: number = root.size();

// check root is recognized as LiveMap TypeScript type
root.get('someKey');
root.size();
// check custom user provided typings via LiveObjectsTypes are working:
// keys on a root:
const aNumber: number = root.get('numberKey');
const aString: string = root.get('stringKey');
const aBoolean: boolean = root.get('booleanKey');
const couldBeUndefined: string | undefined = root.get('couldBeUndefined');
// live objects on a root:
const counter: Ably.LiveCounter | undefined = root.get('counterKey');
const map: LiveObjectsTypes['root']['mapKey'] = root.get('mapKey');
// check string literal types works
// need to use nullish coalescing as we didn't actually create any data on the root,
// so the next calls would fail. we only need to check that TypeScript types work
const foo: 'bar' = map?.get('foo')!;
const baz: 'qux' = map?.get('nestedMap')?.get('baz')!;

// check LiveMap subscription callback has correct TypeScript types
const { unsubscribe } = root.subscribe(({ update }) => {
Expand All @@ -31,13 +51,15 @@ globalThis.testAblyPackage = async function () {
});
unsubscribe();

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

// check can provide custom types for the getRoot method, ignoring global LiveObjectsTypes interface
const explicitRoot: Ably.LiveMap<ExplicitRootType> = await liveObjects.getRoot<ExplicitRootType>();
const someOtherKey: string = explicitRoot.get('someOtherKey');
};
1 change: 1 addition & 0 deletions test/package/browser/template/src/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"include": ["**/*.ts", "**/*.tsx"],
"compilerOptions": {
"strictNullChecks": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"module": "esnext",
Expand Down

0 comments on commit a1e29ff

Please sign in to comment.