From a8dd91ed05edf80ac654770eef5ba66142376d8d Mon Sep 17 00:00:00 2001 From: Daniel Kos Date: Wed, 6 Jun 2018 21:05:34 +0200 Subject: [PATCH] Make login/logout tests compatible with jsdom Feature #13563 Arvados-DCO-1.1-Signed-off-by: Daniel Kos : --- src/services/auth-service/auth-service.ts | 4 ++-- src/store/auth/auth-reducer.test.ts | 28 +++++------------------ yarn.lock | 2 +- 3 files changed, 9 insertions(+), 25 deletions(-) diff --git a/src/services/auth-service/auth-service.ts b/src/services/auth-service/auth-service.ts index 9e75c72a..4b213fb0 100644 --- a/src/services/auth-service/auth-service.ts +++ b/src/services/auth-service/auth-service.ts @@ -47,11 +47,11 @@ export default class AuthService { public login() { const currentUrl = `${window.location.protocol}//${window.location.host}/token`; - window.location.href = `${API_HOST}/login?return_to=${currentUrl}`; + window.location.assign(`${API_HOST}/login?return_to=${currentUrl}`); } public logout() { const currentUrl = `${window.location.protocol}//${window.location.host}`; - window.location.href = `${API_HOST}/logout?return_to=${currentUrl}`; + window.location.assign(`${API_HOST}/logout?return_to=${currentUrl}`); } } diff --git a/src/store/auth/auth-reducer.test.ts b/src/store/auth/auth-reducer.test.ts index 9290f57d..f2e20baa 100644 --- a/src/store/auth/auth-reducer.test.ts +++ b/src/store/auth/auth-reducer.test.ts @@ -12,7 +12,7 @@ import { } from "../../services/auth-service/auth-service"; import { API_HOST } from "../../common/server-api"; -require('jest-localstorage-mock'); +import 'jest-localstorage-mock'; describe('auth-reducer', () => { beforeAll(() => { @@ -84,34 +84,18 @@ describe('auth-reducer', () => { it('should fire external url to login', () => { const initialState = undefined; - - const location = { - href: 'http://localhost:3000', - protocol: 'http:', - host: 'localhost:3000' - }; - - global['window'] = { location }; - + window.location.assign = jest.fn(); authReducer(initialState, actions.LOGIN()); - expect(window.location.href).toBe( - `${API_HOST}/login?return_to=${location.protocol}//${location.host}/token` + expect(window.location.assign).toBeCalledWith( + `${API_HOST}/login?return_to=${window.location.protocol}//${window.location.host}/token` ); }); it('should fire external url to logout', () => { const initialState = undefined; - - const location = { - href: 'http://localhost:3000', - protocol: 'http:', - host: 'localhost:3000' - }; - - global['window'] = { location }; - + window.location.assign = jest.fn(); authReducer(initialState, actions.LOGOUT()); - expect(window.location.href).toBe( + expect(window.location.assign).toBeCalledWith( `${API_HOST}/logout?return_to=${location.protocol}//${location.host}` ); }); diff --git a/yarn.lock b/yarn.lock index 17549e01..7b9820bb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4003,7 +4003,7 @@ jest-leak-detector@^22.4.0: dependencies: pretty-format "^22.4.3" -jest-localstorage-mock@^2.2.0: +jest-localstorage-mock@2.2.0: version "2.2.0" resolved "https://registry.yarnpkg.com/jest-localstorage-mock/-/jest-localstorage-mock-2.2.0.tgz#ce9a9de01dfdde2ad8aa08adf73acc7e5cc394cf" -- 2.30.2