Skip to content

Commit 32838f3

Browse files
committed
Add LiveObjects plugin test to the package tests
Test LiveObjects plugin can be imported and provided to the Ably client, and that TypeScript types for LiveObjects work as expected.
1 parent bf0ffeb commit 32838f3

File tree

7 files changed

+64
-5
lines changed

7 files changed

+64
-5
lines changed

test/package/browser/template/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This directory is intended to be used for testing the following aspects of the a
88
It contains three files, each of which import ably-js in different manners, and provide a way to briefly exercise its functionality:
99

1010
- `src/index-default.ts` imports the default ably-js package (`import { Realtime } from 'ably'`).
11+
- `src/index-liveobjects.ts` imports the LiveObjects ably-js plugin (`import LiveObjects from 'ably/liveobjects'`).
1112
- `src/index-modular.ts` imports the tree-shakable ably-js package (`import { BaseRealtime, WebSocketTransport, FetchRequest } from 'ably/modular'`).
1213
- `src/ReactApp.tsx` imports React hooks from the ably-js package (`import { useChannel } from 'ably/react'`).
1314

@@ -25,6 +26,7 @@ This directory exposes three package scripts that are to be used for testing:
2526

2627
- `build`: Uses esbuild to create:
2728
1. a bundle containing `src/index-default.ts` and ably-js;
28-
2. a bundle containing `src/index-modular.ts` and ably-js.
29+
2. a bundle containing `src/index-liveobjects.ts` and ably-js.
30+
3. a bundle containing `src/index-modular.ts` and ably-js.
2931
- `test`: Using the bundles created by `build` and playwright components setup, tests that the code that exercises ably-js’s functionality is working correctly in a browser.
3032
- `typecheck`: Type-checks the code that imports ably-js.

test/package/browser/template/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"build": "esbuild --bundle src/index-default.ts --outdir=dist && esbuild --bundle src/index-modular.ts --outdir=dist",
7+
"build": "esbuild --bundle src/index-default.ts --outdir=dist && esbuild --bundle src/index-liveobjects.ts --outdir=dist && esbuild --bundle src/index-modular.ts --outdir=dist",
88
"typecheck": "tsc --project src -noEmit",
99
"test-support:server": "ts-node server/server.ts",
1010
"test": "npm run test:lib && npm run test:hooks",
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>Ably NPM package test (LiveObjects plugin export)</title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="index-liveobjects.js"></script>
9+
<script type="text/javascript" src="runTest.js"></script>
10+
</body>
11+
</html>

test/package/browser/template/server/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ async function startWebServer(listenPort: number) {
55
const server = express();
66
server.get('/', (req, res) => res.send('OK'));
77
server.use(express.static(path.join(__dirname, '/resources')));
8-
for (const filename of ['index-default.js', 'index-modular.js']) {
8+
for (const filename of ['index-default.js', 'index-liveobjects.js', 'index-modular.js']) {
99
server.use(`/${filename}`, express.static(path.join(__dirname, '..', 'dist', filename)));
1010
}
1111

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import * as Ably from 'ably';
2+
import LiveObjects from 'ably/liveobjects';
3+
import { createSandboxAblyAPIKey } from './sandbox';
4+
5+
globalThis.testAblyPackage = async function () {
6+
const key = await createSandboxAblyAPIKey({ featureFlags: ['enableChannelState'] });
7+
8+
const realtime = new Ably.Realtime({ key, environment: 'sandbox', plugins: { LiveObjects } });
9+
10+
const channel = realtime.channels.get('channel', { modes: ['STATE_SUBSCRIBE', 'STATE_PUBLISH'] });
11+
// check liveObjects can be accessed
12+
const liveObjects = channel.liveObjects;
13+
const root = await liveObjects.getRoot();
14+
15+
// check root is recognized as LiveMap TypeScript type
16+
root.get('someKey');
17+
root.size();
18+
19+
// check LiveMap subscription callback has correct TypeScript types
20+
const { unsubscribe } = root.subscribe(({ update }) => {
21+
switch (update.someKey) {
22+
case 'removed':
23+
case 'updated':
24+
break;
25+
default:
26+
// check all possible types are exhausted
27+
const shouldExhaustAllTypes: never = update.someKey;
28+
}
29+
});
30+
unsubscribe();
31+
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
36+
counter?.value();
37+
const counterSubscribeResponse = counter?.subscribe(({ update }) => {
38+
const shouldBeANumber: number = update.inc;
39+
});
40+
counterSubscribeResponse?.unsubscribe();
41+
};

test/package/browser/template/src/sandbox.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import testAppSetup from '../../../../common/ably-common/test-resources/test-app-setup.json';
22

3-
export async function createSandboxAblyAPIKey() {
3+
export async function createSandboxAblyAPIKey(withOptions?: object) {
4+
const postData = {
5+
...testAppSetup.post_apps,
6+
...(withOptions ?? {}),
7+
};
48
const response = await fetch('https://sandbox-rest.ably.io/apps', {
59
method: 'POST',
610
headers: { 'Content-Type': 'application/json' },
7-
body: JSON.stringify(testAppSetup.post_apps),
11+
body: JSON.stringify(postData),
812
});
913

1014
if (!response.ok) {

test/package/browser/template/test/lib/package.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { test, expect } from '@playwright/test';
33
test.describe('NPM package', () => {
44
for (const scenario of [
55
{ name: 'default export', path: '/index-default.html' },
6+
{ name: 'LiveObjects plugin export', path: '/index-liveobjects.html' },
67
{ name: 'modular export', path: '/index-modular.html' },
78
]) {
89
test.describe(scenario.name, () => {

0 commit comments

Comments
 (0)