From f70fb12c3da4d1fea2d05f697b2a36fe40ca2216 Mon Sep 17 00:00:00 2001 From: Paul Armstrong Date: Fri, 12 Feb 2021 14:23:23 -0800 Subject: [PATCH] test(web): NavigationDrawer --- web/src/components/NavigationDrawer.jsx | 3 +- .../__tests__/NavigationDrawer.test.jsx | 61 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 web/src/components/__tests__/NavigationDrawer.test.jsx diff --git a/web/src/components/NavigationDrawer.jsx b/web/src/components/NavigationDrawer.jsx index faec1d71c..417e7fb77 100644 --- a/web/src/components/NavigationDrawer.jsx +++ b/web/src/components/NavigationDrawer.jsx @@ -12,9 +12,10 @@ export default function NavigationDrawer({ children, header }) { return ( - {showDrawer ?
: ''} + {showDrawer ?
: ''}
{ + let useDrawer, setShowDrawer; + + beforeEach(() => { + setShowDrawer = jest.fn(); + useDrawer = jest.spyOn(Context, 'useDrawer').mockImplementation(() => ({ showDrawer: true, setShowDrawer })); + }); + + test('renders a navigation drawer', async () => { + render( + +
Hello
+
+ ); + expect(screen.queryByTestId('children')).toHaveTextContent('Hello'); + expect(screen.queryByTestId('drawer').classList.contains('translate-x-full')).toBe(false); + expect(screen.queryByTestId('drawer').classList.contains('translate-x-0')).toBe(true); + }); + + test('is dismissed when the scrim is clicked', async () => { + useDrawer + .mockReturnValueOnce({ showDrawer: true, setShowDrawer }) + .mockReturnValueOnce({ showDrawer: false, setShowDrawer }); + render(); + fireEvent.click(screen.queryByTestId('scrim')); + expect(setShowDrawer).toHaveBeenCalledWith(false); + }); + + test('is not visible when not set to show', async () => { + useDrawer.mockReturnValue({ showDrawer: false, setShowDrawer }); + render(); + expect(screen.queryByTestId('scrim')).not.toBeInTheDocument(); + expect(screen.queryByTestId('drawer').classList.contains('-translate-x-full')).toBe(true); + expect(screen.queryByTestId('drawer').classList.contains('translate-x-0')).toBe(false); + }); +}); + +describe('Destination', () => { + let setShowDrawer; + + beforeEach(() => { + setShowDrawer = jest.fn(); + jest.spyOn(Context, 'useDrawer').mockImplementation(() => ({ showDrawer: true, setShowDrawer })); + }); + + test('dismisses the drawer moments after being clicked', async () => { + render( + + + + ); + fireEvent.click(screen.queryByText('Tacos')); + jest.runAllTimers(); + expect(setShowDrawer).toHaveBeenCalledWith(false); + }); +});