-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from AlexStack/nextjs14
Nextjs14
- Loading branch information
Showing
7 changed files
with
122 additions
and
57 deletions.
There are no files selected for viewing
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
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
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
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,22 @@ | ||
import { ReactNode } from 'react'; | ||
|
||
export interface FetchApiContext { | ||
topError: ReactNode; | ||
fetchCount: number; | ||
} | ||
|
||
export const FETCH_API_CTX_VALUE: FetchApiContext = { | ||
topError: null, | ||
fetchCount: 0, | ||
}; | ||
|
||
// You can add more context interface & values here and use them in different places | ||
export interface AnotherContext { | ||
someValue: string; | ||
secondValue?: number; | ||
} | ||
|
||
export const ANOTHER_CTX_VALUE: AnotherContext = { | ||
someValue: 'default value', | ||
secondValue: 0, | ||
}; |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './config'; | ||
export * from './context'; | ||
export * from './env'; |
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 |
---|---|---|
@@ -1,56 +1,84 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import React, { act } from 'react'; | ||
|
||
import { ClientProvider, useClientContext } from './useClientContext'; | ||
import { | ||
ClientProvider, | ||
OUTSIDE_CLIENT_PROVIDER_ERROR, | ||
useClientContext, | ||
} from './useClientContext'; | ||
|
||
describe('useClientContext', () => { | ||
it('should not be used outside ClientProvider', () => { | ||
const { result } = renderHook(() => useClientContext()); | ||
expect(() => { | ||
result.current.updateClientCtx({ fetchCount: 66 }); | ||
}).toThrow('Cannot be used outside ClientProvider'); | ||
try { | ||
renderHook(() => useClientContext()); | ||
} catch (error) { | ||
expect(error).toEqual(new Error(OUTSIDE_CLIENT_PROVIDER_ERROR)); | ||
} | ||
}); | ||
|
||
it('should provide the correct initial context values', () => { | ||
const defaultCtxValue = { | ||
status: 'Pending', | ||
topError: '', | ||
fetchCount: 0, | ||
}; | ||
const ctxValue = { | ||
topError: 'SWW Error', | ||
bmStatus: 'Live', | ||
status: 'Live', | ||
fetchCount: 85, | ||
}; | ||
const wrapper = ({ children }: { children: React.ReactNode }) => ( | ||
<ClientProvider value={ctxValue}>{children}</ClientProvider> | ||
<ClientProvider value={ctxValue} defaultValue={defaultCtxValue}> | ||
{children} | ||
</ClientProvider> | ||
); | ||
|
||
const { result } = renderHook(() => useClientContext(), { | ||
wrapper, | ||
}); | ||
const { result } = renderHook( | ||
() => useClientContext<typeof defaultCtxValue>(), | ||
{ | ||
wrapper, | ||
} | ||
); | ||
|
||
expect(result.current.topError).toBe(ctxValue.topError); | ||
expect(result.current.fetchCount).toBe(ctxValue.fetchCount); | ||
}); | ||
|
||
it('should update the context values', () => { | ||
const defaultCtxValue = { | ||
picUrl: '', | ||
loading: false, | ||
total: 0, | ||
}; | ||
const ctxValue = { | ||
topError: 'SWW Error', | ||
fetchCount: 85, | ||
picUrl: 'https://picsum.photos/300/160', | ||
loading: true, | ||
total: 3, | ||
}; | ||
const wrapper = ({ children }: { children: React.ReactNode }) => ( | ||
<ClientProvider value={ctxValue}>{children}</ClientProvider> | ||
<ClientProvider value={ctxValue} defaultValue={defaultCtxValue}> | ||
{children} | ||
</ClientProvider> | ||
); | ||
|
||
const { result } = renderHook(() => useClientContext(), { | ||
wrapper, | ||
}); | ||
const { result } = renderHook( | ||
() => useClientContext<typeof defaultCtxValue>(), | ||
{ | ||
wrapper, | ||
} | ||
); | ||
|
||
const newCtxValue = { | ||
topError: '', | ||
picUrl: 'https://picsum.photos/200/150', | ||
loading: false, | ||
}; | ||
|
||
act(() => { | ||
result.current.updateClientCtx(newCtxValue); | ||
}); | ||
|
||
expect(result.current.topError).toBe(newCtxValue.topError); | ||
expect(result.current.fetchCount).toBe(ctxValue.fetchCount); | ||
expect(result.current.picUrl).toBe(newCtxValue.picUrl); | ||
expect(result.current.total).toBe(ctxValue.total); // not updated | ||
expect(result.current.loading).toBe(newCtxValue.loading); | ||
}); | ||
}); |
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