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 19, 2024
1 parent 02281b3 commit 5d79580
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
19 changes: 19 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,19 @@
import { LiveCounter, LiveMap } from 'ably';

declare global {
export interface LiveObjectsTypes {
root: {
numberKey: number;
stringKey: string;
booleanKey: boolean;
couldBeUndefined?: string;
mapKey?: LiveMap<{
foo: 'bar';
nestedMap?: LiveMap<{
baz: 'qux';
}>;
}>;
counterKey?: LiveCounter;
};
}
}
24 changes: 19 additions & 5 deletions test/package/browser/template/src/index-liveobjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import * as Ably from 'ably';
import LiveObjects from 'ably/liveobjects';
import { createSandboxAblyAPIKey } from './sandbox';

// TODO: check we can refer to types exported by LiveObjects plugin.

globalThis.testAblyPackage = async function () {
const key = await createSandboxAblyAPIKey();

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

// check root is recognized as LiveMap TypeScript type
root.get('someKey');
root.size();

// check custom user provided typings from ably.config.d.ts 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 }) => {
switch (update.someKey) {
Expand All @@ -29,10 +45,8 @@ 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
// check LiveCounter type also behaves as expected
// same deal with nullish coalescing
counter?.value();
const counterSubscribeResponse = counter?.subscribe(({ update }) => {
const shouldBeANumber: number = update.inc;
Expand Down
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 5d79580

Please sign in to comment.