Skip to content

Commit 774366b

Browse files
committed
Add feature flags to tests, fix collection-card
1 parent 011438a commit 774366b

File tree

4 files changed

+142
-12
lines changed

4 files changed

+142
-12
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { FeatureFlags } from '@audius/common/services'
2+
3+
import { createMockLocalStorage } from './local-storage'
4+
import { createMockRemoteConfig } from './remote-config'
5+
6+
export const createMockAppContext = (
7+
featureFlags: Partial<Record<FeatureFlags, boolean>> = {}
8+
) => ({
9+
analytics: {
10+
track: async () => {},
11+
make: () => ({ eventName: '', properties: {} })
12+
},
13+
imageUtils: {
14+
generatePlaylistArtwork: async () => ({ url: '', file: new File([], '') })
15+
},
16+
getHostUrl: () => 'http://localhost:3000',
17+
audiusBackend: {} as any,
18+
trackDownload: {} as any,
19+
audiusSdk: undefined,
20+
remoteConfig: createMockRemoteConfig(featureFlags),
21+
localStorage: createMockLocalStorage()
22+
})
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
export const createMockLocalStorage = () => {
2+
const storage = new Map<string, string>()
3+
const mockLocalStorage = {
4+
getItem: (key: string) => storage.get(key) ?? null,
5+
setItem: (key: string, value: string) => {
6+
storage.set(key, value)
7+
},
8+
removeItem: (key: string) => {
9+
storage.delete(key)
10+
}
11+
}
12+
13+
return {
14+
localStorage: mockLocalStorage,
15+
getItem: async (key: string) => storage.get(key) ?? null,
16+
setItem: async (key: string, value: string) => {
17+
storage.set(key, value)
18+
},
19+
removeItem: async (key: string) => {
20+
storage.delete(key)
21+
},
22+
getValue: async (key: string) => storage.get(key) ?? null,
23+
setValue: async (key: string, value: string) => {
24+
storage.set(key, value)
25+
},
26+
getJSONValue: async (key: string) => {
27+
const value = storage.get(key)
28+
return value ? JSON.parse(value) : null
29+
},
30+
getExpiringJSONValue: async (key: string) => {
31+
const value = storage.get(key)
32+
return value ? JSON.parse(value) : null
33+
},
34+
setJSONValue: async (key: string, value: any) => {
35+
storage.set(key, JSON.stringify(value))
36+
},
37+
setExpiringJSONValue: async (key: string, value: any) => {
38+
storage.set(key, JSON.stringify(value))
39+
},
40+
clear: async () => {
41+
storage.clear()
42+
},
43+
removeJSONValue: async (key: string) => {
44+
storage.delete(key)
45+
},
46+
removeExpiringJSONValue: async (key: string) => {
47+
storage.delete(key)
48+
},
49+
getKeysThatMatch: async (pattern: string) => {
50+
return Array.from(storage.keys()).filter((key) => key.match(pattern))
51+
},
52+
getAll: async () => {
53+
return Object.fromEntries(storage.entries())
54+
},
55+
getAllJSON: async () => {
56+
const entries = Array.from(storage.entries())
57+
return Object.fromEntries(
58+
entries.map(([key, value]) => [key, JSON.parse(value)])
59+
)
60+
},
61+
getAllExpiringJSON: async () => {
62+
const entries = Array.from(storage.entries())
63+
return Object.fromEntries(
64+
entries.map(([key, value]) => [key, JSON.parse(value)])
65+
)
66+
},
67+
getAudiusAccount: async () => null,
68+
setAudiusAccount: async () => {},
69+
clearAudiusAccount: async () => {},
70+
getAudiusAccountUser: async () => null,
71+
setAudiusAccountUser: async () => {},
72+
clearAudiusAccountUser: async () => {},
73+
clearAudiusUserWalletOverride: async () => {},
74+
getAudiusUserWalletOverride: async () => null,
75+
setAudiusUserWalletOverride: async () => {},
76+
getAudiusUserWalletAddress: async () => null,
77+
setAudiusUserWalletAddress: async () => {},
78+
clearAudiusUserWalletAddress: async () => {},
79+
clearPlaybackRate: async () => {}
80+
}
81+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { FeatureFlags } from '@audius/common/services'
2+
3+
export const createMockRemoteConfig = (
4+
featureFlags: Partial<Record<FeatureFlags, boolean>> = {}
5+
) => ({
6+
getFeatureEnabled: (flag: FeatureFlags, fallbackFlag?: FeatureFlags) => {
7+
return (
8+
featureFlags[flag] ?? featureFlags[fallbackFlag as FeatureFlags] ?? false
9+
)
10+
},
11+
getRemoteVar: () => null,
12+
waitForRemoteConfig: async () => {},
13+
waitForUserRemoteConfig: async () => {},
14+
getEagerRemoteConfig: () => null,
15+
setUserId: () => {},
16+
listenForUserId: () => {},
17+
unlistenForUserId: () => {},
18+
init: async () => {}
19+
})

packages/web/src/test/test-utils.tsx

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {
44
AudiusQueryContext,
55
AudiusQueryContextType
66
} from '@audius/common/audius-query'
7+
import { AppContext } from '@audius/common/context'
8+
import { FeatureFlags } from '@audius/common/services'
79
import { ThemeProvider } from '@audius/harmony'
810
import { QueryClientProvider } from '@tanstack/react-query'
911
import { render, RenderOptions } from '@testing-library/react'
@@ -24,8 +26,11 @@ import { queryClient } from 'services/query-client'
2426
import { configureStore } from 'store/configureStore'
2527
import { AppState } from 'store/types'
2628

29+
import { createMockAppContext } from './mocks/app-context'
30+
2731
type TestOptions = {
2832
reduxState?: PartialDeep<AppState>
33+
featureFlags?: Partial<Record<FeatureFlags, boolean>>
2934
}
3035

3136
type ReduxProviderProps = {
@@ -48,28 +53,31 @@ type TestProvidersProps = {
4853
children: ReactNode
4954
}
5055

51-
const audiusQueryContext = {} as unknown as AudiusQueryContextType
52-
5356
const TestProviders =
5457
(options?: TestOptions) => (props: TestProvidersProps) => {
5558
const { children } = props
56-
const { reduxState } = options ?? {}
59+
const { reduxState, featureFlags } = options ?? {}
60+
const mockAppContext = createMockAppContext(featureFlags)
61+
const audiusQueryContext = {} as unknown as AudiusQueryContextType
62+
5763
return (
5864
<HistoryContextProvider>
5965
<QueryClientProvider client={queryClient}>
6066
<AudiusQueryContext.Provider value={audiusQueryContext}>
6167
<ThemeProvider theme='day'>
6268
<ReduxProvider initialStoreState={reduxState}>
6369
<RouterContextProvider>
64-
<ToastContextProvider>
65-
<HistoryContext.Consumer>
66-
{({ history }) => (
67-
<Router history={history}>
68-
<CompatRouter>{children}</CompatRouter>
69-
</Router>
70-
)}
71-
</HistoryContext.Consumer>
72-
</ToastContextProvider>
70+
<AppContext.Provider value={mockAppContext}>
71+
<ToastContextProvider>
72+
<HistoryContext.Consumer>
73+
{({ history }) => (
74+
<Router history={history}>
75+
<CompatRouter>{children}</CompatRouter>
76+
</Router>
77+
)}
78+
</HistoryContext.Consumer>
79+
</ToastContextProvider>
80+
</AppContext.Provider>
7381
</RouterContextProvider>
7482
</ReduxProvider>
7583
</ThemeProvider>

0 commit comments

Comments
 (0)