-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
6 changed files
with
58 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
test/package/browser/template/server/resources/index-liveobjects.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Ably NPM package test (LiveObjects plugin export)</title> | ||
</head> | ||
<body> | ||
<script type="text/javascript" src="index-liveobjects.js"></script> | ||
<script type="text/javascript" src="runTest.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import * as Ably from 'ably'; | ||
import LiveObjects from 'ably/liveobjects'; | ||
import { createSandboxAblyAPIKey } from './sandbox'; | ||
|
||
globalThis.testAblyPackage = async function () { | ||
const key = await createSandboxAblyAPIKey(); | ||
|
||
const realtime = new Ably.Realtime({ key, environment: 'sandbox', plugins: { LiveObjects } }); | ||
|
||
const channel = realtime.channels.get('channel', { modes: ['STATE_SUBSCRIBE', 'STATE_PUBLISH'] }); | ||
// check liveObjects can be accessed | ||
const liveObjects = channel.liveObjects; | ||
const root = await liveObjects.getRoot(); | ||
|
||
// check root is recognized as LiveMap TypeScript type | ||
root.get('someKey'); | ||
root.size(); | ||
|
||
// check LiveMap subscription callback has correct TypeScript types | ||
const { unsubscribe } = root.subscribe(({ update }) => { | ||
switch (update.someKey) { | ||
case 'removed': | ||
case 'updated': | ||
break; | ||
default: | ||
// check all possible types are exhausted | ||
const shouldExhaustAllTypes: never = update.someKey; | ||
} | ||
}); | ||
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 | ||
counter?.value(); | ||
const counterSubscribeResponse = counter?.subscribe(({ update }) => { | ||
const shouldBeANumber: number = update.inc; | ||
}); | ||
counterSubscribeResponse?.unsubscribe(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters