Skip to content

Commit

Permalink
Fix SSR errors with fetch polyfill usage (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
luisrudge authored Aug 28, 2019
1 parent acf6c8e commit e7ed091
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
50 changes: 27 additions & 23 deletions __tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
decodeState,
sha256,
openPopup,
oauthToken,
runPopup,
runIframe,
urlDecodeB64
Expand Down Expand Up @@ -164,20 +163,27 @@ describe('utils', () => {
});
});
describe('oauthToken', () => {
let oauthToken;
let mockUnfetch;
beforeEach(() => {
jest.resetModules();
jest.mock('unfetch');
mockUnfetch = require('unfetch');
oauthToken = require('../src/utils').oauthToken;
});
it('calls oauth/token with the correct url', async () => {
(<any>global).fetch = jest.fn(
() =>
new Promise(res =>
res({ ok: true, json: () => new Promise(ress => ress(true)) })
)
mockUnfetch.mockReturnValue(
new Promise(res =>
res({ ok: true, json: () => new Promise(ress => ress(true)) })
)
);
await oauthToken({
baseUrl: 'https://test.com',
client_id: 'client_idIn',
code: 'codeIn',
code_verifier: 'code_verifierIn'
});
expect(fetch).toHaveBeenCalledWith('https://test.com/oauth/token', {
expect(mockUnfetch).toHaveBeenCalledWith('https://test.com/oauth/token', {
body:
'{"grant_type":"authorization_code","redirect_uri":"http://localhost","client_id":"client_idIn","code":"codeIn","code_verifier":"code_verifierIn"}',
headers: { 'Content-type': 'application/json' },
Expand All @@ -189,14 +195,13 @@ describe('utils', () => {
error: 'the-error',
error_description: 'the-error-description'
};
(<any>global).fetch = jest.fn(
() =>
new Promise(res =>
res({
ok: false,
json: () => new Promise(ress => ress(theError))
})
)
mockUnfetch.mockReturnValue(
new Promise(res =>
res({
ok: false,
json: () => new Promise(ress => ress(theError))
})
)
);
try {
await oauthToken({
Expand All @@ -212,14 +217,13 @@ describe('utils', () => {
}
});
it('handles error without error response', async () => {
(<any>global).fetch = jest.fn(
() =>
new Promise(res =>
res({
ok: false,
json: () => new Promise(ress => ress(false))
})
)
mockUnfetch.mockReturnValue(
new Promise(res =>
res({
ok: false,
json: () => new Promise(ress => ress(false))
})
)
);
try {
await oauthToken({
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'core-js/es/typed-array/slice';
import 'core-js/es/array/includes';
import 'promise-polyfill/src/polyfill';
import 'fast-text-encoding';
import 'unfetch/polyfill/index';

import Auth0Client from './Auth0Client';
import * as ClientStorage from './storage';
Expand Down
2 changes: 2 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as qs from 'qss';
import fetch from 'unfetch';

import { DEFAULT_AUTHORIZE_TIMEOUT_IN_SECONDS } from './constants';

const dedupe = arr => arr.filter((x, i) => arr.indexOf(x) === i);
Expand Down

0 comments on commit e7ed091

Please sign in to comment.