Skip to content

Commit

Permalink
update tests related to browser headers
Browse files Browse the repository at this point in the history
  • Loading branch information
swellmike committed Jul 30, 2024
1 parent 56a5f21 commit bbb5fbd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ async function request(
headers: reqHeaders,
body: reqBody,
// Credentials and mode are only available in the browser
...(typeof window !== 'undefined' && window.document
...(!utils.isServer()
? {
credentials: 'include',
mode: 'cors',
Expand Down
36 changes: 32 additions & 4 deletions src/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,48 @@ describe('api', () => {
});
});

it('should make a fetch request with credentials', async () => {
it('should make a fetch request without credentials', async () => {
await api.request('get', '/test');

expect(fetch.mock.calls.length).toEqual(1);
expect(fetch.mock.calls[0][1]).toHaveProperty('credentials', 'include');
expect(fetch.mock.calls[0][1]).not.toHaveProperty(
'credentials',
'include',
);
});

it('should make a fetch request with cors', async () => {
it('should make a fetch request without cors', async () => {
await api.request('get', '/test');

expect(fetch.mock.calls.length).toEqual(1);
expect(fetch.mock.calls[0][1]).toHaveProperty('mode', 'cors');
expect(fetch.mock.calls[0][1]).not.toHaveProperty('mode', 'cors');
});

describe('when making a request from the browser', () => {
beforeEach(() => {
// simulate browser
global.window = { document: {} };
});

afterEach(() => {
delete global.window;
});

it('should make a fetch request with credentials', async () => {
await api.request('get', '/test');

expect(fetch.mock.calls.length).toEqual(1);
expect(fetch.mock.calls[0][1]).toHaveProperty('credentials', 'include');
});

it('should make a fetch request with cors', async () => {
await api.request('get', '/test');

expect(fetch.mock.calls.length).toEqual(1);
expect(fetch.mock.calls[0][1]).toHaveProperty('mode', 'cors');
});
}); // describe: when making a request from the browser

it('should trim url', async () => {
await api.request('get', '///test///');

Expand Down
2 changes: 1 addition & 1 deletion src/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function getCookie(name) {
return undefined;
}

const matches = document.cookie.match(
const matches = window.document.cookie?.match(
new RegExp(
'(?:^|; )' + name.replace(/([.$?*|{}()[]\\\/\+^])/g, '\\$1') + '=([^;]*)',
),
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function reduce(arr, cb, init) {
}

function isServer() {
return !(typeof window !== 'undefined' && window && window.document);
return !(typeof window !== 'undefined' && window?.document);
}

function isFunction(func) {
Expand Down

0 comments on commit bbb5fbd

Please sign in to comment.