Skip to content

Commit

Permalink
test(lazy): behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Sep 9, 2020
1 parent e8ae70a commit 865a357
Showing 1 changed file with 117 additions and 0 deletions.
117 changes: 117 additions & 0 deletions src/tests/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import WebSocket from 'ws';
import { url, startServer, pubsub } from './fixtures/simple';
import { Server } from '../server';
import { createClient } from '../client';
import { noop } from '../utils';

/** Waits for the specified timeout and then resolves the promise. */
const wait = (timeout: number) =>
Expand Down Expand Up @@ -300,3 +301,119 @@ describe('"concurrency"', () => {
expect(nextFnForBananas).toBeCalled();
});
});

describe('lazy', () => {
it('should connect immediately when mode is disabled', async () => {
createClient({
url,
lazy: false,
});
await wait(5);

expect(server.webSocketServer.clients.size).toBe(1);
server.webSocketServer.clients.forEach((client) => {
expect(client.readyState).toBe(WebSocket.OPEN);
});
});

it('should close socket when disposing while mode is disabled', async () => {
const client = createClient({
url,
lazy: false,
});
await wait(5);

client.dispose();
await wait(5);

expect(server.webSocketServer.clients.size).toBe(0);
});

it('should connect on first subscribe when mode is enabled', async () => {
const client = createClient({
url,
lazy: true, // default
});
await wait(5);

expect(server.webSocketServer.clients.size).toBe(0);

client.subscribe(
{
operationName: 'BoughtBananas',
query: `subscription BoughtBananas {
boughtBananas {
name
}
}`,
variables: {},
},
{
next: noop,
error: noop,
complete: noop,
},
);
await wait(5);

expect(server.webSocketServer.clients.size).toBe(1);
server.webSocketServer.clients.forEach((client) => {
expect(client.readyState).toBe(WebSocket.OPEN);
});
});

it('should disconnect on last unsubscribe when mode is enabled', async () => {
const client = createClient({
url,
lazy: true, // default
});
await wait(5);

const disposeClient1 = client.subscribe(
{
operationName: 'BoughtBananas',
query: `subscription BoughtBananas {
boughtBananas {
name
}
}`,
variables: {},
},
{
next: noop,
error: noop,
complete: noop,
},
);
await wait(5);

const disposeClient2 = client.subscribe(
{
operationName: 'BecomingHappy',
query: `subscription BecomingHappy {
becameHappy {
name
}
}`,
variables: {},
},
{
next: noop,
error: noop,
complete: noop,
},
);
await wait(5);

disposeClient1();
await wait(5);

// still connected
expect(server.webSocketServer.clients.size).toBe(1);

// everyone unsubscribed
disposeClient2();
await wait(5);
expect(server.webSocketServer.clients.size).toBe(0);
});
});

0 comments on commit 865a357

Please sign in to comment.