-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(react): remove direct usage of react-test-renderer and types (#…
- Loading branch information
1 parent
09d7d54
commit 825cccc
Showing
27 changed files
with
320 additions
and
403 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@aws-amplify/ui-react-core-notifications": patch | ||
"@aws-amplify/ui-react-core": patch | ||
"@aws-amplify/ui-react-liveness": patch | ||
"@aws-amplify/ui-react-native": patch | ||
--- | ||
|
||
chore(react): remove direct usage of react-test-renderer and types |
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
14 changes: 0 additions & 14 deletions
14
...e-notifications/src/InAppMessaging/context/InAppMessagingContext/InAppMessagingContext.ts
This file was deleted.
Oops, something went wrong.
49 changes: 49 additions & 0 deletions
49
...-notifications/src/InAppMessaging/context/InAppMessagingContext/InAppMessagingContext.tsx
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,49 @@ | ||
import React from 'react'; | ||
import { InAppMessage } from '../../types'; | ||
import { useInAppMessagingState } from './useInAppMessagingState'; | ||
|
||
export interface InAppMessagingContextType { | ||
clearMessage: () => void; | ||
displayMessage: (message: InAppMessage) => void; | ||
message: InAppMessage | null; | ||
} | ||
|
||
const InAppMessagingContext = | ||
React.createContext<InAppMessagingContextType | null>(null); | ||
|
||
export default InAppMessagingContext; | ||
|
||
export interface InAppMessagingProviderProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
export interface UseInAppMessaging extends InAppMessagingContextType {} | ||
|
||
export const ERROR_MESSAGE = | ||
'`useInAppMessaging` must be called from within `InAppMessagingProvider`'; | ||
|
||
/** | ||
* Utility hook used to access the InAppMessagingContext values | ||
* | ||
* @returns {InAppMessagingContextType} InAppMessaging context values | ||
*/ | ||
export function useInAppMessaging(): UseInAppMessaging { | ||
const context = React.useContext(InAppMessagingContext); | ||
|
||
if (!context) { | ||
throw new Error(ERROR_MESSAGE); | ||
} | ||
return context; | ||
} | ||
|
||
export function InAppMessagingProvider({ | ||
children, | ||
}: InAppMessagingProviderProps): React.JSX.Element { | ||
const value = useInAppMessagingState(); | ||
|
||
return ( | ||
<InAppMessagingContext.Provider value={value}> | ||
{children} | ||
</InAppMessagingContext.Provider> | ||
); | ||
} |
53 changes: 53 additions & 0 deletions
53
...src/InAppMessaging/context/InAppMessagingContext/__tests__/InAppMessagingContext.spec.tsx
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,53 @@ | ||
import React from 'react'; | ||
import { renderHook } from '@testing-library/react'; | ||
|
||
import { InAppMessagingContextType } from '..'; | ||
|
||
import { | ||
ERROR_MESSAGE, | ||
InAppMessagingProvider, | ||
useInAppMessaging, | ||
} from '../InAppMessagingContext'; | ||
|
||
const clearMessage = jest.fn(); | ||
const displayMessage = jest.fn(); | ||
const message = null; | ||
|
||
jest.mock('../useInAppMessagingState', () => ({ | ||
useInAppMessagingState: (): InAppMessagingContextType => ({ | ||
clearMessage, | ||
displayMessage, | ||
message, | ||
}), | ||
})); | ||
|
||
jest.mock('aws-amplify/in-app-messaging'); | ||
|
||
describe('useInAppMessaging', () => { | ||
it('provides the values of InAppMessagingContext to consumers wrapped in InAppMessagingProvider', () => { | ||
const { result } = renderHook(() => useInAppMessaging(), { | ||
wrapper: ({ children }) => ( | ||
<InAppMessagingProvider>{children}</InAppMessagingProvider> | ||
), | ||
}); | ||
|
||
expect(result.current).toStrictEqual({ | ||
clearMessage, | ||
displayMessage, | ||
message, | ||
}); | ||
}); | ||
|
||
it('throws an error when called outside an InAppMessagingProvider', () => { | ||
/* eslint-disable no-console */ | ||
// monkeypatch console.error into a no-op temporarily to supress React printed error regarding lack of | ||
// ErrorBoundary component | ||
const original = console.error; | ||
console.error = () => {}; | ||
|
||
expect(() => renderHook(() => useInAppMessaging())).toThrow(ERROR_MESSAGE); | ||
|
||
console.error = original; | ||
/* eslint-enable no-console */ | ||
}); | ||
}); |
Oops, something went wrong.