-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wiggle toasts on update + toast.wiggle()
- Loading branch information
1 parent
c01f5bd
commit de60ddc
Showing
12 changed files
with
690 additions
and
366 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
This file was deleted.
Oops, something went wrong.
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,168 @@ | ||
import { View } from 'react-native'; | ||
import type { ToastProps } from '../types'; // Adjust the import path Adjust the import path | ||
import { areToastsEqual } from '../toast-comparator'; | ||
|
||
// Mock helper function to simulate a click handler | ||
const mockClickHandler = () => {}; | ||
|
||
describe('areToastsEqual', () => { | ||
it('should return true when all important properties are equal', () => { | ||
const toast1: ToastProps = { | ||
id: 1, | ||
title: 'Toast 1', | ||
variant: 'success', | ||
description: 'This is a toast', | ||
closeButton: true, | ||
invert: false, | ||
position: 'top-center', | ||
dismissible: true, | ||
action: { label: 'Retry', onClick: mockClickHandler }, | ||
cancel: { label: 'Cancel', onClick: mockClickHandler }, | ||
}; | ||
|
||
const toast2: ToastProps = { | ||
id: 1, | ||
title: 'Toast 1', | ||
variant: 'success', | ||
description: 'This is a toast', | ||
closeButton: true, | ||
invert: false, | ||
position: 'top-center', | ||
dismissible: true, | ||
action: { label: 'Retry', onClick: mockClickHandler }, | ||
cancel: { label: 'Cancel', onClick: mockClickHandler }, | ||
}; | ||
|
||
expect(areToastsEqual(toast1, toast2)).toBe(true); | ||
}); | ||
|
||
it('should return false when IDs are different', () => { | ||
const toast1: ToastProps = { | ||
id: 1, | ||
title: 'Toast 1', | ||
variant: 'success', | ||
description: 'This is a toast', | ||
}; | ||
|
||
const toast2: ToastProps = { | ||
id: 2, | ||
title: 'Toast 1', | ||
variant: 'success', | ||
description: 'This is a toast', | ||
}; | ||
|
||
expect(areToastsEqual(toast1, toast2)).toBe(false); | ||
}); | ||
|
||
it('should return false when titles are different', () => { | ||
const toast1: ToastProps = { | ||
id: 1, | ||
title: 'Toast 1', | ||
variant: 'success', | ||
description: 'This is a toast', | ||
}; | ||
|
||
const toast2: ToastProps = { | ||
id: 1, | ||
title: 'Toast 2', | ||
variant: 'success', | ||
description: 'This is a toast', | ||
}; | ||
|
||
expect(areToastsEqual(toast1, toast2)).toBe(false); | ||
}); | ||
|
||
it('should return false when variants are different', () => { | ||
const toast1: ToastProps = { | ||
id: 1, | ||
title: 'Toast 1', | ||
variant: 'success', | ||
description: 'This is a toast', | ||
}; | ||
|
||
const toast2: ToastProps = { | ||
id: 1, | ||
title: 'Toast 1', | ||
variant: 'error', | ||
description: 'This is a toast', | ||
}; | ||
|
||
expect(areToastsEqual(toast1, toast2)).toBe(false); | ||
}); | ||
|
||
it('should return false when descriptions are different', () => { | ||
const toast1: ToastProps = { | ||
id: 1, | ||
title: 'Toast 1', | ||
variant: 'success', | ||
description: 'This is a toast', | ||
}; | ||
|
||
const toast2: ToastProps = { | ||
id: 1, | ||
title: 'Toast 1', | ||
variant: 'success', | ||
description: 'This is another toast', | ||
}; | ||
|
||
expect(areToastsEqual(toast1, toast2)).toBe(false); | ||
}); | ||
|
||
it('should return false when action labels are different', () => { | ||
const toast1: ToastProps = { | ||
id: 1, | ||
title: 'Toast 1', | ||
variant: 'success', | ||
action: { label: 'Retry', onClick: mockClickHandler }, | ||
}; | ||
|
||
const toast2: ToastProps = { | ||
id: 1, | ||
title: 'Toast 1', | ||
variant: 'success', | ||
action: { label: 'Ignore', onClick: mockClickHandler }, | ||
}; | ||
|
||
expect(areToastsEqual(toast1, toast2)).toBe(false); | ||
}); | ||
|
||
it('should return true when action and cancel are both React nodes and equal', () => { | ||
const mockReactNode = <View />; | ||
|
||
const toast1: ToastProps = { | ||
id: 1, | ||
title: 'Toast 1', | ||
variant: 'success', | ||
action: mockReactNode, | ||
cancel: mockReactNode, | ||
}; | ||
|
||
const toast2: ToastProps = { | ||
id: 1, | ||
title: 'Toast 1', | ||
variant: 'success', | ||
action: mockReactNode, | ||
cancel: mockReactNode, | ||
}; | ||
|
||
expect(areToastsEqual(toast1, toast2)).toBe(true); | ||
}); | ||
|
||
it('should return false when cancel labels are different', () => { | ||
const toast1: ToastProps = { | ||
id: 1, | ||
title: 'Toast 1', | ||
variant: 'success', | ||
cancel: { label: 'Cancel', onClick: mockClickHandler }, | ||
}; | ||
|
||
const toast2: ToastProps = { | ||
id: 1, | ||
title: 'Toast 1', | ||
variant: 'success', | ||
cancel: { label: 'Dismiss', onClick: mockClickHandler }, | ||
}; | ||
|
||
expect(areToastsEqual(toast1, toast2)).toBe(false); | ||
}); | ||
}); |
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,25 @@ | ||
import { isToastAction, type ToastProps } from './types'; | ||
|
||
const areActionsEqual = (a: ToastProps['action'], b: ToastProps['action']) => { | ||
if (isToastAction(a) && isToastAction(b)) { | ||
if (a.label !== b.label) return false; | ||
return true; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
export const areToastsEqual = (a: ToastProps, b: ToastProps) => { | ||
return ( | ||
a.id === b.id && | ||
a.title === b.title && | ||
a.variant === b.variant && | ||
a.description === b.description && | ||
a.closeButton === b.closeButton && | ||
a.invert === b.invert && | ||
a.position === b.position && | ||
a.dismissible === b.dismissible && | ||
areActionsEqual(a.action, b.action) && | ||
areActionsEqual(a.cancel, b.cancel) | ||
); | ||
}; |
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
Oops, something went wrong.