Skip to content

Commit

Permalink
test: added a test for #166
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Jul 31, 2022
1 parent a14d2d9 commit 281aedd
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion packages/batch/test/batch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { rest } from 'msw';
import { server } from '../../villus/test/mocks/server';
import { batch } from '../src/index';
import { mount } from '../../villus/test/helpers/mount';
import { useClient, useQuery } from '../../villus/src';
import { useClient, useQuery, definePlugin } from '../../villus/src';
import waitForExpect from 'wait-for-expect';
import { PostQuery, PostsQuery, QueryErrorWith500, QueryWithNetworkError } from 'villus/test/mocks/queries';

Expand Down Expand Up @@ -181,4 +181,45 @@ describe('batch plugin', () => {
expect(fetch).toHaveBeenCalledTimes(3);
});
});

// #166
test('can set fetch options', async () => {
const headerPlugin = definePlugin(({ opContext }) => {
opContext.headers['X-CUSTOM-HEADER'] = 'TEST';
opContext.credentials = 'include';
});

mount({
setup() {
useClient({
url: 'https://test.com/graphql',
use: [headerPlugin, batch()],
});

useQuery({ query: PostsQuery });

return {};
},
template: `<div></div>`,
});

await flushPromises();
jest.advanceTimersByTime(100);
await flushPromises();

// wait-for-expect uses timers under the hood, so we need to reset here
jest.useRealTimers();
await waitForExpect(() => {
expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenLastCalledWith(
expect.any(String),
expect.objectContaining({
credentials: 'include',
headers: expect.objectContaining({
'X-CUSTOM-HEADER': 'TEST',
}),
})
);
});
});
});

0 comments on commit 281aedd

Please sign in to comment.