Skip to content

Commit 1e85e80

Browse files
committed
#280 Add test for fetching resources in node
1 parent 44ffe18 commit 1e85e80

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.34.11",
2+
"version": "0.34.12",
33
"author": "Joep Meindertsma",
44
"dependencies": {
55
"@noble/ed25519": "1.6.0",

lib/src/store.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,18 @@ describe('Store', () => {
1515
.toString();
1616
expect(atomString).to.equal(testval);
1717
});
18+
19+
it('fetches a resource', async () => {
20+
const store = new Store({ serverUrl: 'https://atomicdata.dev' });
21+
const resource = await store.getResourceAsync(
22+
'https://atomicdata.dev/properties/createdAt',
23+
);
24+
25+
if (resource.error) {
26+
throw resource.error;
27+
}
28+
29+
const atomString = resource.get(urls.properties.shortname)!.toString();
30+
expect(atomString).to.equal('created-at');
31+
});
1832
});

lib/src/store.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import { authenticate, fetchWebSocket, startWebsocket } from './websockets.js';
1515
/** Function called when a resource is updated or removed */
1616
type Callback = (resource: Resource) => void;
1717

18+
/** Returns True if the client has WebSocket support */
19+
const supportsWebSockets = () => typeof WebSocket !== 'undefined';
20+
1821
export enum StoreEvents {
1922
/**
2023
* Whenever `Resource.save()` is called, so only when the user of this library
@@ -167,7 +170,11 @@ export class Store {
167170
// Use WebSocket if available, else use HTTP(S)
168171
const ws = this.getWebSocketForSubject(subject);
169172

170-
if (!opts.noWebSocket && WebSocket && ws?.readyState === WebSocket.OPEN) {
173+
if (
174+
!opts.noWebSocket &&
175+
supportsWebSockets() &&
176+
ws?.readyState === WebSocket.OPEN
177+
) {
171178
fetchWebSocket(ws, subject);
172179
} else {
173180
fetchResource(
@@ -433,13 +440,13 @@ export class Store {
433440

434441
this.serverUrl = url;
435442
// TODO This is not the right place
436-
this.openWebSocket(url);
443+
supportsWebSockets() && this.openWebSocket(url);
437444
}
438445

439446
/** Opens a WebSocket for this Atomic Server URL */
440447
public openWebSocket(url: string) {
441448
// Check if we're running in a webbrowser
442-
if (typeof window !== 'undefined') {
449+
if (supportsWebSockets()) {
443450
if (this.webSockets.has(url)) {
444451
return;
445452
}

react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.34.11",
2+
"version": "0.34.12",
33
"author": "Joep Meindertsma",
44
"description": "Atomic Data React library",
55
"dependencies": {

0 commit comments

Comments
 (0)