-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
115 additions
and
38 deletions.
There are no files selected for viewing
33 changes: 0 additions & 33 deletions
33
src/newHome/homeOnboarding/onboardingContext/OnboardingStorage.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
src/newHome/homeOnboarding/onboardingContext/useOnboardingStorage.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { useUser } from "@dashboard/auth"; | ||
import { useUpdateMetadataMutation } from "@dashboard/graphql"; | ||
import { act } from "@testing-library/react"; | ||
import { renderHook } from "@testing-library/react-hooks"; | ||
|
||
import { useOnboardingStorage } from "./useOnboardingStorage"; | ||
|
||
jest.mock("@dashboard/auth", () => ({ | ||
__esModule: true, | ||
useUser: jest.fn(), | ||
})); | ||
|
||
jest.mock("@dashboard/graphql"); | ||
|
||
jest.useFakeTimers(); | ||
|
||
jest.mock("lodash/debounce", () => jest.fn(fn => fn)); | ||
|
||
describe("useOnboardingStorage", () => { | ||
describe("getOnboardingState", () => { | ||
it("should return undefined when there is no onboarding in user metadata", () => { | ||
// Arrange | ||
(useUser as jest.Mock).mockImplementation(() => ({ | ||
user: { metadata: [{ key1: "value1" }, { key2: "value2" }] }, | ||
})); | ||
(useUpdateMetadataMutation as jest.Mock).mockReturnValue([jest.fn(), {}]); | ||
|
||
const { getOnboardingState } = renderHook(() => useOnboardingStorage()).result.current; | ||
|
||
// Act | ||
const result = getOnboardingState(); | ||
|
||
// Assert | ||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
it("should return onboarding state from user metadata", () => { | ||
// Arrange | ||
(useUser as jest.Mock).mockImplementation(() => ({ | ||
user: { | ||
metadata: [ | ||
{ | ||
key: "onboarding", | ||
value: JSON.stringify({ steps: [], onboardingExpanded: true }), | ||
}, | ||
], | ||
}, | ||
})); | ||
(useUpdateMetadataMutation as jest.Mock).mockReturnValue([jest.fn(), {}]); | ||
|
||
const { getOnboardingState } = renderHook(() => useOnboardingStorage()).result.current; | ||
|
||
// Act | ||
const result = getOnboardingState(); | ||
|
||
// Assert | ||
expect(result).toEqual({ steps: [], onboardingExpanded: true }); | ||
}); | ||
}); | ||
|
||
describe("saveOnboardingState", () => { | ||
it("should not save onboarding state when there is no user", async () => { | ||
// Arrange | ||
(useUser as jest.Mock).mockImplementation(() => ({ user: null })); | ||
|
||
const updateMetadataMock = jest.fn(); | ||
|
||
(useUpdateMetadataMutation as jest.Mock).mockReturnValue([updateMetadataMock, {}]); | ||
|
||
const { result } = renderHook(() => useOnboardingStorage()); | ||
|
||
// Act | ||
const returnValue = await act(async () => { | ||
return await result.current.saveOnboardingState({ steps: [], onboardingExpanded: true }); | ||
}); | ||
|
||
// Assert | ||
expect(returnValue).toBeUndefined(); | ||
expect(updateMetadataMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should save onboarding state to user metadata and be called only once", async () => { | ||
// Arrange | ||
(useUser as jest.Mock).mockImplementation(() => ({ user: { id: "1", metadata: [] } })); | ||
|
||
const updateMetadataMock = jest.fn(); | ||
|
||
(useUpdateMetadataMutation as jest.Mock).mockReturnValue([updateMetadataMock, {}]); | ||
|
||
const { result } = renderHook(() => useOnboardingStorage()); | ||
|
||
// Act | ||
await act(async () => { | ||
await result.current.saveOnboardingState({ steps: [], onboardingExpanded: true }); | ||
}); | ||
|
||
jest.runAllTimers(); | ||
|
||
// Assert | ||
expect(updateMetadataMock).toHaveBeenCalledTimes(1); | ||
expect(updateMetadataMock).toHaveBeenCalledWith({ | ||
variables: { | ||
id: "1", | ||
input: [ | ||
{ key: "onboarding", value: JSON.stringify({ steps: [], onboardingExpanded: true }) }, | ||
], | ||
keysToDelete: [], | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters