|
| 1 | +import React from 'react'; |
| 2 | +import { Meta, StoryObj } from '@storybook/react'; |
| 3 | +import { configureStore } from '@reduxjs/toolkit'; |
| 4 | +import { Provider } from 'react-redux'; |
| 5 | +import authStateReducer, { AuthState } from 'store/slices/authState'; |
| 6 | +import { AuthDependantRender } from './AuthDependantRender'; |
| 7 | + |
| 8 | +const createMockStore = (authState: AuthState) => |
| 9 | + configureStore({ |
| 10 | + reducer: { |
| 11 | + authState: authStateReducer, |
| 12 | + }, |
| 13 | + preloadedState: { |
| 14 | + authState, |
| 15 | + }, |
| 16 | + }); |
| 17 | + |
| 18 | +const withMockedAuthState = (authState: AuthState) => { |
| 19 | + const Decorator = (Story: any) => ( |
| 20 | + <Provider store={createMockStore(authState)}> |
| 21 | + <Story /> |
| 22 | + </Provider> |
| 23 | + ); |
| 24 | + Decorator.displayName = `withMockedAuthState(${authState.authState})`; |
| 25 | + return Decorator; |
| 26 | +}; |
| 27 | + |
| 28 | +const meta = { args: { children: 'Visible' }, component: AuthDependantRender } satisfies Meta< |
| 29 | + typeof AuthDependantRender |
| 30 | +>; |
| 31 | + |
| 32 | +export default meta; |
| 33 | +type Story = StoryObj<typeof meta>; |
| 34 | + |
| 35 | +//renderOn: loggedIn |
| 36 | +export const RenderOnLoggedInStateLoggedIn: Story = { |
| 37 | + args: { |
| 38 | + renderOn: 'loggedIn', |
| 39 | + }, |
| 40 | + decorators: [withMockedAuthState({ authState: 'loggedIn' })], |
| 41 | +}; |
| 42 | + |
| 43 | +export const RenderOnLoggedInStateLoggedOut: Story = { |
| 44 | + args: { |
| 45 | + renderOn: 'loggedIn', |
| 46 | + }, |
| 47 | + decorators: [withMockedAuthState({ authState: 'loggedOut' })], |
| 48 | +}; |
| 49 | + |
| 50 | +export const RenderOnLoggedInStateWaiting: Story = { |
| 51 | + args: { |
| 52 | + renderOn: 'loggedIn', |
| 53 | + }, |
| 54 | + decorators: [withMockedAuthState({ authState: 'waiting' })], |
| 55 | +}; |
| 56 | + |
| 57 | +//renderOn: loggedOut |
| 58 | +export const RenderOnLoggedOutStateLoggedIn: Story = { |
| 59 | + args: { |
| 60 | + renderOn: 'loggedOut', |
| 61 | + }, |
| 62 | + decorators: [withMockedAuthState({ authState: 'loggedIn' })], |
| 63 | +}; |
| 64 | + |
| 65 | +export const RenderOnLoggedOutStateLoggedOut: Story = { |
| 66 | + args: { |
| 67 | + renderOn: 'loggedOut', |
| 68 | + }, |
| 69 | + decorators: [withMockedAuthState({ authState: 'loggedOut' })], |
| 70 | +}; |
| 71 | + |
| 72 | +export const RenderOnLoggedOutStateWaiting: Story = { |
| 73 | + args: { |
| 74 | + renderOn: 'loggedOut', |
| 75 | + }, |
| 76 | + decorators: [withMockedAuthState({ authState: 'waiting' })], |
| 77 | +}; |
| 78 | + |
| 79 | +//renderOn: waiting |
| 80 | +export const RenderOnWaitingStateLoggedIn: Story = { |
| 81 | + args: { |
| 82 | + renderOn: 'waiting', |
| 83 | + }, |
| 84 | + decorators: [withMockedAuthState({ authState: 'loggedIn' })], |
| 85 | +}; |
| 86 | + |
| 87 | +export const RenderOnWaitingStateLoggedOut: Story = { |
| 88 | + args: { |
| 89 | + renderOn: 'waiting', |
| 90 | + }, |
| 91 | + decorators: [withMockedAuthState({ authState: 'loggedOut' })], |
| 92 | +}; |
| 93 | + |
| 94 | +export const RenderOnWaitingStateWaiting: Story = { |
| 95 | + args: { |
| 96 | + renderOn: 'waiting', |
| 97 | + }, |
| 98 | + decorators: [withMockedAuthState({ authState: 'waiting' })], |
| 99 | +}; |
| 100 | + |
| 101 | +//renderOn: always |
| 102 | +export const RenderOnAlwaysStateLoggedIn: Story = { |
| 103 | + args: { |
| 104 | + renderOn: 'always', |
| 105 | + }, |
| 106 | + decorators: [withMockedAuthState({ authState: 'loggedIn' })], |
| 107 | +}; |
| 108 | + |
| 109 | +export const RenderOnAlwaysStateLoggedOut: Story = { |
| 110 | + args: { |
| 111 | + renderOn: 'always', |
| 112 | + }, |
| 113 | + decorators: [withMockedAuthState({ authState: 'loggedOut' })], |
| 114 | +}; |
| 115 | + |
| 116 | +export const RenderOnAlwaysStateWaiting: Story = { |
| 117 | + args: { |
| 118 | + renderOn: 'always', |
| 119 | + }, |
| 120 | + decorators: [withMockedAuthState({ authState: 'waiting' })], |
| 121 | +}; |
0 commit comments