soapbox/app/soapbox/actions/__tests__/onboarding.test.ts

101 wiersze
2.8 KiB
TypeScript
Czysty Zwykły widok Historia

2022-07-06 22:02:44 +00:00
import { mockStore, mockWindowProperty, rootState } from 'soapbox/jest/test-helpers';
2022-04-12 13:51:28 +00:00
2022-05-02 20:55:52 +00:00
import { checkOnboardingStatus, startOnboarding, endOnboarding } from '../onboarding';
describe('checkOnboarding()', () => {
let mockGetItem: any;
mockWindowProperty('localStorage', {
getItem: (key: string) => mockGetItem(key),
});
beforeEach(() => {
2023-09-16 20:16:59 +00:00
mockGetItem = vi.fn().mockReturnValue(null);
2022-05-02 20:55:52 +00:00
});
it('does nothing if localStorage item is not set', async() => {
2023-09-16 20:16:59 +00:00
mockGetItem = vi.fn().mockReturnValue(null);
2022-05-02 20:55:52 +00:00
2022-07-06 22:02:44 +00:00
const state = rootState.setIn(['onboarding', 'needsOnboarding'], false);
2022-05-02 20:55:52 +00:00
const store = mockStore(state);
await store.dispatch(checkOnboardingStatus());
const actions = store.getActions();
expect(actions).toEqual([]);
expect(mockGetItem.mock.calls.length).toBe(1);
});
it('does nothing if localStorage item is invalid', async() => {
2023-09-16 20:16:59 +00:00
mockGetItem = vi.fn().mockReturnValue('invalid');
2022-05-02 20:55:52 +00:00
2022-07-06 22:02:44 +00:00
const state = rootState.setIn(['onboarding', 'needsOnboarding'], false);
2022-05-02 20:55:52 +00:00
const store = mockStore(state);
await store.dispatch(checkOnboardingStatus());
const actions = store.getActions();
expect(actions).toEqual([]);
expect(mockGetItem.mock.calls.length).toBe(1);
});
it('dispatches the correct action', async() => {
2023-09-16 20:16:59 +00:00
mockGetItem = vi.fn().mockReturnValue('1');
2022-05-02 20:55:52 +00:00
2022-07-06 22:02:44 +00:00
const state = rootState.setIn(['onboarding', 'needsOnboarding'], false);
2022-05-02 20:55:52 +00:00
const store = mockStore(state);
await store.dispatch(checkOnboardingStatus());
const actions = store.getActions();
expect(actions).toEqual([{ type: 'ONBOARDING_START' }]);
expect(mockGetItem.mock.calls.length).toBe(1);
});
});
describe('startOnboarding()', () => {
let mockSetItem: any;
mockWindowProperty('localStorage', {
setItem: (key: string, value: string) => mockSetItem(key, value),
});
beforeEach(() => {
2023-09-16 20:16:59 +00:00
mockSetItem = vi.fn();
2022-05-02 20:55:52 +00:00
});
it('dispatches the correct action', async() => {
2022-07-06 22:02:44 +00:00
const state = rootState.setIn(['onboarding', 'needsOnboarding'], false);
2022-05-02 20:55:52 +00:00
const store = mockStore(state);
await store.dispatch(startOnboarding());
const actions = store.getActions();
expect(actions).toEqual([{ type: 'ONBOARDING_START' }]);
expect(mockSetItem.mock.calls.length).toBe(1);
});
});
2022-04-12 13:51:28 +00:00
describe('endOnboarding()', () => {
2022-05-02 20:55:52 +00:00
let mockRemoveItem: any;
2022-04-12 13:51:28 +00:00
2022-05-02 20:55:52 +00:00
mockWindowProperty('localStorage', {
removeItem: (key: string) => mockRemoveItem(key),
});
2022-04-12 13:51:28 +00:00
2022-05-02 20:55:52 +00:00
beforeEach(() => {
2023-09-16 20:16:59 +00:00
mockRemoveItem = vi.fn();
2022-05-02 20:55:52 +00:00
});
2022-04-12 13:51:28 +00:00
2022-05-02 20:55:52 +00:00
it('dispatches the correct action', async() => {
2022-07-06 22:02:44 +00:00
const state = rootState.setIn(['onboarding', 'needsOnboarding'], false);
2022-05-02 20:55:52 +00:00
const store = mockStore(state);
await store.dispatch(endOnboarding());
const actions = store.getActions();
2022-05-02 20:55:52 +00:00
expect(actions).toEqual([{ type: 'ONBOARDING_END' }]);
expect(mockRemoveItem.mock.calls.length).toBe(1);
2022-04-12 13:51:28 +00:00
});
});