From fb0be7808337e0f6dd51051903844fb2ab48a0b3 Mon Sep 17 00:00:00 2001 From: Bhavin789 Date: Wed, 13 Oct 2021 23:31:30 +0530 Subject: [PATCH 1/8] test: add UT for reducer Login popup --- src/redux/reducer.test.ts | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/redux/reducer.test.ts diff --git a/src/redux/reducer.test.ts b/src/redux/reducer.test.ts new file mode 100644 index 0000000..0e0edc3 --- /dev/null +++ b/src/redux/reducer.test.ts @@ -0,0 +1,39 @@ +import { reducer } from "./reducer"; + +import { AuthState } from "./action"; + +describe("test reducer", () => { + const mockUser = { + CreatedDate: "mock-created-date", + Email: [ + { + Type: "mock-email-type", + Value: "mock-email-value", + }, + ], + EmailVerified: true, + ID: "mock-id", + IsActive: true, + IsDeleted: true, + LastLoginDate: "mock-login-date", + LastLoginLocation: "mock-last-login-action", + ModifiedDate: "mock-modified-date", + PasswordExpirationDate: "mock-password-expiration", + Provider: "mock-provider", + RegistrationProvider: "-mock-registration-provider", + RegistrationSource: "mock-registration-source", + SignupDate: "mock-signup-date", + Uid: "mock-uid", + Description: "mock-error-description", + ErrorCode: 403, + Message: "mock-error-message", + }; + test("should return isLoading true on LOGIN_POPUP_STARTED action", () => { + const mockState: AuthState = { + isAuthenticated: true, + isLoading: false, + }; + const state = reducer(mockState, { type: "LOGIN_POPUP_STARTED" }); + expect(state).toEqual({ isAuthenticated: true, isLoading: true }); + }); +}); From 55c3b833b45c33f11c406667dc09e5d01dc63185 Mon Sep 17 00:00:00 2001 From: Bhavin789 Date: Wed, 13 Oct 2021 23:31:57 +0530 Subject: [PATCH 2/8] fix: resolve lint error --- src/redux/reducer.test.ts | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/src/redux/reducer.test.ts b/src/redux/reducer.test.ts index 0e0edc3..a876c8f 100644 --- a/src/redux/reducer.test.ts +++ b/src/redux/reducer.test.ts @@ -3,31 +3,6 @@ import { reducer } from "./reducer"; import { AuthState } from "./action"; describe("test reducer", () => { - const mockUser = { - CreatedDate: "mock-created-date", - Email: [ - { - Type: "mock-email-type", - Value: "mock-email-value", - }, - ], - EmailVerified: true, - ID: "mock-id", - IsActive: true, - IsDeleted: true, - LastLoginDate: "mock-login-date", - LastLoginLocation: "mock-last-login-action", - ModifiedDate: "mock-modified-date", - PasswordExpirationDate: "mock-password-expiration", - Provider: "mock-provider", - RegistrationProvider: "-mock-registration-provider", - RegistrationSource: "mock-registration-source", - SignupDate: "mock-signup-date", - Uid: "mock-uid", - Description: "mock-error-description", - ErrorCode: 403, - Message: "mock-error-message", - }; test("should return isLoading true on LOGIN_POPUP_STARTED action", () => { const mockState: AuthState = { isAuthenticated: true, From de477ecacc4c7ceee0674913b1ebd5a0bb5410f5 Mon Sep 17 00:00:00 2001 From: Bhavin789 Date: Wed, 13 Oct 2021 23:37:31 +0530 Subject: [PATCH 3/8] test: add UT for Get access token action --- src/redux/reducer.test.ts | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/redux/reducer.test.ts b/src/redux/reducer.test.ts index a876c8f..d356ff7 100644 --- a/src/redux/reducer.test.ts +++ b/src/redux/reducer.test.ts @@ -3,6 +3,31 @@ import { reducer } from "./reducer"; import { AuthState } from "./action"; describe("test reducer", () => { + const mockUser = { + CreatedDate: "mock-created-date", + Email: [ + { + Type: "mock-email-type", + Value: "mock-email-value", + }, + ], + EmailVerified: true, + ID: "mock-id", + IsActive: true, + IsDeleted: true, + LastLoginDate: "mock-login-date", + LastLoginLocation: "mock-last-login-action", + ModifiedDate: "mock-modified-date", + PasswordExpirationDate: "mock-password-expiration", + Provider: "mock-provider", + RegistrationProvider: "-mock-registration-provider", + RegistrationSource: "mock-registration-source", + SignupDate: "mock-signup-date", + Uid: "mock-uid", + Description: "mock-error-description", + ErrorCode: 403, + Message: "mock-error-message", + }; test("should return isLoading true on LOGIN_POPUP_STARTED action", () => { const mockState: AuthState = { isAuthenticated: true, @@ -11,4 +36,20 @@ describe("test reducer", () => { const state = reducer(mockState, { type: "LOGIN_POPUP_STARTED" }); expect(state).toEqual({ isAuthenticated: true, isLoading: true }); }); + + test("should return same state if user modified date is same on GET_ACCESS_TOKEN_COMPLETE action", () => { + const mockState: AuthState = { + isAuthenticated: true, + isLoading: false, + user: mockUser, + }; + const state = reducer(mockState, { + type: "GET_ACCESS_TOKEN_COMPLETE", + user: { + ...mockUser, + ModifiedDate: "mock-modified-date", + }, + }); + expect(state).toEqual(mockState); + }); }); From 71840b7b4cea3380fc70a4b5b710baee3b3ff073 Mon Sep 17 00:00:00 2001 From: Bhavin789 Date: Wed, 13 Oct 2021 23:42:00 +0530 Subject: [PATCH 4/8] test: add UT for get access token with different user --- src/redux/reducer.test.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/redux/reducer.test.ts b/src/redux/reducer.test.ts index d356ff7..9a235bc 100644 --- a/src/redux/reducer.test.ts +++ b/src/redux/reducer.test.ts @@ -52,4 +52,23 @@ describe("test reducer", () => { }); expect(state).toEqual(mockState); }); + + test("should return state with new user if user modified date is different on GET_ACCESS_TOKEN_COMPLETE action", () => { + const mockState: AuthState = { + isAuthenticated: true, + isLoading: false, + user: mockUser, + }; + + const mockActionUser = { + ...mockUser, + ModifiedDate: "different-modified-date", + id: "mock-different-id", + }; + const state = reducer(mockState, { + type: "GET_ACCESS_TOKEN_COMPLETE", + user: mockActionUser, + }); + expect(state).toEqual({ ...mockState, user: mockActionUser }); + }); }); From 02c85d358a2ffd3a7aefa7e1e518e6bd1fae5308 Mon Sep 17 00:00:00 2001 From: Bhavin789 Date: Wed, 13 Oct 2021 23:44:02 +0530 Subject: [PATCH 5/8] test: add UT for LOGOUT action --- src/redux/reducer.test.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/redux/reducer.test.ts b/src/redux/reducer.test.ts index 9a235bc..c4377cf 100644 --- a/src/redux/reducer.test.ts +++ b/src/redux/reducer.test.ts @@ -71,4 +71,21 @@ describe("test reducer", () => { }); expect(state).toEqual({ ...mockState, user: mockActionUser }); }); + + test("should un-authenticate user on logout on LOGOUT action", () => { + const mockState: AuthState = { + isAuthenticated: true, + isLoading: false, + user: mockUser, + }; + + const state = reducer(mockState, { + type: "LOGOUT", + }); + expect(state).toEqual({ + ...mockState, + isAuthenticated: false, + user: undefined, + }); + }); }); From 453be8fed2dfd2058bd9cf0ee2950ec9701fb4fe Mon Sep 17 00:00:00 2001 From: Bhavin789 Date: Wed, 13 Oct 2021 23:46:46 +0530 Subject: [PATCH 6/8] test: add UT for ERROR action --- src/redux/reducer.test.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/redux/reducer.test.ts b/src/redux/reducer.test.ts index c4377cf..cdb830d 100644 --- a/src/redux/reducer.test.ts +++ b/src/redux/reducer.test.ts @@ -88,4 +88,28 @@ describe("test reducer", () => { user: undefined, }); }); + + test("should set action error on ERROR action", () => { + const mockState: AuthState = { + isAuthenticated: true, + isLoading: false, + user: mockUser, + }; + + const mockActionError = { + name: "mock-name", + message: "mock-message", + stack: "mock-stack", + }; + + const state = reducer(mockState, { + type: "ERROR", + error: mockActionError, + }); + expect(state).toEqual({ + ...mockState, + isLoading: false, + error: mockActionError, + }); + }); }); From 3ff4d0095087e0cc8ea509d2ef4dd90834ee722d Mon Sep 17 00:00:00 2001 From: Bhavin789 Date: Wed, 13 Oct 2021 23:48:54 +0530 Subject: [PATCH 7/8] test: add UT for LOGIN_POPUP_COMPLETE action --- src/redux/reducer.test.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/redux/reducer.test.ts b/src/redux/reducer.test.ts index cdb830d..5978dc8 100644 --- a/src/redux/reducer.test.ts +++ b/src/redux/reducer.test.ts @@ -72,6 +72,30 @@ describe("test reducer", () => { expect(state).toEqual({ ...mockState, user: mockActionUser }); }); + test("should complete login on LOGIN_POPUP_COMPLETE action", () => { + const mockState: AuthState = { + isAuthenticated: true, + isLoading: false, + user: mockUser, + }; + + const mockActionUser = { + ...mockUser, + ModifiedDate: "different-modified-date", + id: "mock-different-id", + }; + const state = reducer(mockState, { + type: "LOGIN_POPUP_COMPLETE", + user: mockActionUser, + }); + expect(state).toEqual({ + ...mockState, + isLoading: false, + error: undefined, + user: mockActionUser, + }); + }); + test("should un-authenticate user on logout on LOGOUT action", () => { const mockState: AuthState = { isAuthenticated: true, From c77c7287083783626b08c70b7b8a853328f1131e Mon Sep 17 00:00:00 2001 From: Bhavin789 Date: Wed, 13 Oct 2021 23:51:38 +0530 Subject: [PATCH 8/8] test: add UT for HANDLE_REDIRECT_COMPLETE action --- src/redux/reducer.test.ts | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/redux/reducer.test.ts b/src/redux/reducer.test.ts index 5978dc8..29da01a 100644 --- a/src/redux/reducer.test.ts +++ b/src/redux/reducer.test.ts @@ -136,4 +136,42 @@ describe("test reducer", () => { error: mockActionError, }); }); + + test("should reset state on INITIALISED action", () => { + const mockState: AuthState = { + isAuthenticated: true, + isLoading: false, + user: mockUser, + }; + + const state = reducer(mockState, { + type: "INITIALISED", + }); + expect(state).toEqual({ + ...mockState, + isLoading: false, + isAuthenticated: false, + error: undefined, + user: undefined, + }); + }); + + test("should reset state on HANDLE_REDIRECT_COMPLETE action", () => { + const mockState: AuthState = { + isAuthenticated: true, + isLoading: false, + user: mockUser, + }; + + const state = reducer(mockState, { + type: "HANDLE_REDIRECT_COMPLETE", + }); + expect(state).toEqual({ + ...mockState, + isLoading: false, + isAuthenticated: false, + error: undefined, + user: undefined, + }); + }); });