Skip to content

Commit

Permalink
add test for fetchSource
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Aug 12, 2021
1 parent 6ed141f commit d699304
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 3 deletions.
163 changes: 162 additions & 1 deletion packages/core/src/internal/fetchSource.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { pipe, subscribe, toPromise } from 'wonka';

import { queryOperation } from '../test-utils';
import { queryOperation, context } from '../test-utils';
import { makeFetchSource } from './fetchSource';
import { gql } from '../gql';
import { Operation } from '../types';
import { makeOperation } from '../utils';

const fetch = (global as any).fetch as jest.Mock;
const abort = jest.fn();
Expand Down Expand Up @@ -152,3 +155,161 @@ describe('on teardown', () => {
expect(abort).toHaveBeenCalledTimes(1);
});
});

function wrap(text) {
return text.split('\n').join('\r\n');
}

describe('on multipart/mixed', () => {
beforeEach(function () {
fetch.mockResolvedValueOnce({
status: 200,
headers: {
get() {
return 'multipart/mixed';
},
'Content-Type': 'multipart/mixed',
},
body: {
getReader: function () {
let cancelled = false;
const results = [
{
done: false,
value: Buffer.from(wrap(`---`)),
},
{
done: false,
value: Buffer.from(
wrap(`Content-Type: application/json; charset=utf-8
${JSON.stringify({
hasNext: true,
data: {
author: {
id: '1',
name: 'Steve',
__typename: 'Author',
todos: [{ id: '1', text: 'stream', __typename: 'Todo' }],
},
},
})}
---`)
),
},
{
done: false,
value: Buffer.from(
wrap(`---
Content-Type: application/json; charset=utf-8
${JSON.stringify({
path: ['author', 'todos', 1],
data: { id: '2', text: 'defer', __typename: 'Todo' },
hasNext: true,
})}
---`)
),
},
{
done: false,
value: Buffer.from(
wrap(`---
Content-Type: application/json; charset=utf-8
{"hasNext":false}
-----`)
),
},
{ done: true },
];
let count = 0;
return {
cancel: function () {
cancelled = true;
},
read: function () {
if (cancelled) throw new Error('No');

return Promise.resolve(results[count++]);
},
};
},
},
});
});

function wait(ms) {
return new Promise(function (resolve) {
setTimeout(() => {
return resolve({});
}, ms);
});
}

it('listens for more responses', async () => {
const streamedQueryOperation: Operation = makeOperation(
'query',
{
query: gql`
query {
author {
id
name
todos @stream {
id
text
}
}
}
`,
variables: {},
key: 1,
},
context
);

const chunks: any[] = [];
const { unsubscribe } = pipe(
makeFetchSource(streamedQueryOperation, 'https://test.com/graphql', {}),
subscribe(function (data) {
chunks.push(data);
})
);

await wait(10);

expect(chunks.length).toEqual(3);
expect(chunks[0].data).toEqual({
author: {
id: '1',
name: 'Steve',
__typename: 'Author',
todos: [{ id: '1', text: 'stream', __typename: 'Todo' }],
},
});
expect(chunks[1].data).toEqual({
author: {
id: '1',
name: 'Steve',
__typename: 'Author',
todos: [
{ id: '1', text: 'stream', __typename: 'Todo' },
{ id: '2', text: 'defer', __typename: 'Todo' },
],
},
});
expect(chunks[2].data).toEqual({
author: {
id: '1',
name: 'Steve',
__typename: 'Author',
todos: [
{ id: '1', text: 'stream', __typename: 'Todo' },
{ id: '2', text: 'defer', __typename: 'Todo' },
],
},
});
unsubscribe();
});
});
1 change: 0 additions & 1 deletion packages/core/src/internal/fetchSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ const executeIncrementalFetch = (
function next(data: ChunkData): Promise<void> | void {
if (!data.done) {
const chunk = toString(data.value);

let boundaryIndex = chunk.indexOf(boundary);
if (boundaryIndex > -1) {
boundaryIndex += buffer.length;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/test-utils/samples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '../types';
import { makeOperation } from '../utils';

const context: OperationContext = {
export const context: OperationContext = {
fetchOptions: {
method: 'POST',
},
Expand Down

0 comments on commit d699304

Please sign in to comment.