Skip to content

Commit

Permalink
Add tests for shared source behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten committed Apr 22, 2021
1 parent bca99c7 commit 7fd4474
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 1 deletion.
192 changes: 192 additions & 0 deletions packages/core/src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
filter,
toArray,
tap,
take,
} from 'wonka';

import { gql } from './gql';
import { Exchange, Operation, OperationResult } from './types';
import { makeOperation } from './utils';
Expand Down Expand Up @@ -507,3 +509,193 @@ describe('queuing behavior', () => {
unsubscribe();
});
});

describe('shared sources behavior', () => {
beforeEach(() => {
jest.useFakeTimers();
});

afterEach(() => {
jest.useRealTimers();
});

it('replays results from prior operation result as needed', async () => {
const exchange: Exchange = () => ops$ => {
let i = 0;
return pipe(
ops$,
map(op => ({
data: ++i,
operation: op,
})),
delay(1)
);
};

const client = createClient({
url: 'test',
exchanges: [exchange],
});

const resultOne = jest.fn();
const resultTwo = jest.fn();

pipe(client.executeRequestOperation(queryOperation), subscribe(resultOne));

expect(resultOne).toHaveBeenCalledTimes(0);

jest.advanceTimersByTime(1);

expect(resultOne).toHaveBeenCalledTimes(1);
expect(resultOne).toHaveBeenCalledWith({
data: 1,
operation: queryOperation,
});

pipe(client.executeRequestOperation(queryOperation), subscribe(resultTwo));

expect(resultTwo).toHaveBeenCalledWith({
data: 1,
stale: true,
operation: queryOperation,
});

jest.advanceTimersByTime(1);

expect(resultTwo).toHaveBeenCalledWith({
data: 2,
operation: queryOperation,
});
});

it('replayed results are not emitted on the shared source', () => {
const exchange: Exchange = () => ops$ => {
let i = 0;
return pipe(
ops$,
map(op => ({
data: ++i,
operation: op,
})),
take(1)
);
};

const client = createClient({
url: 'test',
exchanges: [exchange],
});

const resultOne = jest.fn();
const resultTwo = jest.fn();

pipe(client.executeRequestOperation(queryOperation), subscribe(resultOne));

pipe(client.executeRequestOperation(queryOperation), subscribe(resultTwo));

expect(resultOne).toHaveBeenCalledTimes(1);
expect(resultTwo).toHaveBeenCalledTimes(1);

expect(resultTwo).toHaveBeenCalledWith({
data: 1,
operation: queryOperation,
stale: true,
});
});

it('does nothing when no operation result has been emitted yet', () => {
const exchange: Exchange = () => ops$ => {
return pipe(
ops$,
map(op => ({ data: 1, operation: op })),
filter(() => false)
);
};

const client = createClient({
url: 'test',
exchanges: [exchange],
});

const resultOne = jest.fn();
const resultTwo = jest.fn();

pipe(client.executeRequestOperation(queryOperation), subscribe(resultOne));

pipe(client.executeRequestOperation(queryOperation), subscribe(resultTwo));

expect(resultOne).toHaveBeenCalledTimes(0);
expect(resultTwo).toHaveBeenCalledTimes(0);
});

it('skips replaying results when a result is emitted immediately', () => {
const exchange: Exchange = () => ops$ => {
let i = 0;
return pipe(
ops$,
map(op => ({ data: ++i, operation: op }))
);
};

const client = createClient({
url: 'test',
exchanges: [exchange],
});

const resultOne = jest.fn();
const resultTwo = jest.fn();

pipe(client.executeRequestOperation(queryOperation), subscribe(resultOne));

expect(resultOne).toHaveBeenCalledWith({
data: 1,
operation: queryOperation,
});

pipe(client.executeRequestOperation(queryOperation), subscribe(resultTwo));

expect(resultTwo).toHaveBeenCalledWith({
data: 2,
operation: queryOperation,
});

expect(resultOne).toHaveBeenCalledWith({
data: 2,
operation: queryOperation,
});
});

it('replays stale results as needed', () => {
const exchange: Exchange = () => ops$ => {
return pipe(
ops$,
map(op => ({ stale: true, data: 1, operation: op })),
take(1)
);
};

const client = createClient({
url: 'test',
exchanges: [exchange],
});

const resultOne = jest.fn();
const resultTwo = jest.fn();

pipe(client.executeRequestOperation(queryOperation), subscribe(resultOne));

expect(resultOne).toHaveBeenCalledWith({
data: 1,
operation: queryOperation,
stale: true,
});

pipe(client.executeRequestOperation(queryOperation), subscribe(resultTwo));

expect(resultTwo).toHaveBeenCalledWith({
data: 1,
operation: queryOperation,
stale: true,
});
});
});
3 changes: 2 additions & 1 deletion packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ export class Client {
(op: Operation) =>
op.kind === operation.kind &&
op.key === operation.key &&
op.context.requestPolicy !== 'cache-only'
(op.context.requestPolicy === 'network-only' ||
op.context.requestPolicy === 'cache-and-network')
),
take(1)
);
Expand Down

0 comments on commit 7fd4474

Please sign in to comment.