diff --git a/src/screens/UserPortal/Home/Home.test.tsx b/src/screens/UserPortal/Home/Home.test.tsx index 105714cfc8..3a7cce56f4 100644 --- a/src/screens/UserPortal/Home/Home.test.tsx +++ b/src/screens/UserPortal/Home/Home.test.tsx @@ -151,40 +151,31 @@ async function wait(ms = 100): Promise { } describe('Testing Home Screen [User Portal]', () => { - jest.mock('utils/getOrganizationId'); - - Object.defineProperty(window, 'matchMedia', { - writable: true, - value: jest.fn().mockImplementation((query) => ({ - matches: false, - media: query, - onchange: null, - addListener: jest.fn(), // Deprecated - removeListener: jest.fn(), // Deprecated - addEventListener: jest.fn(), - removeEventListener: jest.fn(), - dispatchEvent: jest.fn(), - })), + beforeEach(() => { + jest.clearAllMocks(); + jest.spyOn(getOrganizationId, 'default').mockReturnValue(''); + + Object.defineProperty(window, 'matchMedia', { + writable: true, + value: jest.fn().mockImplementation((query) => ({ + matches: false, + media: query, + onchange: null, + addListener: jest.fn(), + removeListener: jest.fn(), + addEventListener: jest.fn(), + removeEventListener: jest.fn(), + dispatchEvent: jest.fn(), + })), + }); }); test('Screen should be rendered properly', async () => { const getOrganizationIdSpy = jest .spyOn(getOrganizationId, 'default') - .mockImplementation(() => { - return ''; - }); + .mockReturnValue(''); - render( - - - - - - - - - - ); + renderComponent(); await wait(); @@ -194,21 +185,9 @@ describe('Testing Home Screen [User Portal]', () => { test('Screen should be rendered properly when user types on the Post Input', async () => { const getOrganizationIdSpy = jest .spyOn(getOrganizationId, 'default') - .mockImplementation(() => { - return ''; - }); + .mockReturnValue(''); - render( - - - - - - - - - - ); + renderComponent(); await wait(); @@ -217,27 +196,15 @@ describe('Testing Home Screen [User Portal]', () => { const randomPostInput = 'This is a test'; userEvent.type(screen.getByTestId('postInput'), randomPostInput); - expect(screen.queryByText(randomPostInput)).toBeInTheDocument(); + expect(screen.getByText(randomPostInput)).toBeInTheDocument(); }); test('Error toast should be visible when user tries to create a post with an empty body', async () => { const getOrganizationIdSpy = jest .spyOn(getOrganizationId, 'default') - .mockImplementation(() => { - return ''; - }); + .mockReturnValue(''); - render( - - - - - - - - - - ); + renderComponent(); await wait(); @@ -245,7 +212,7 @@ describe('Testing Home Screen [User Portal]', () => { userEvent.click(screen.getByTestId('postAction')); - expect(toast.error).toBeCalledWith( + expect(toast.error).toHaveBeenCalledWith( "Can't create a post with an empty body." ); }); @@ -253,21 +220,9 @@ describe('Testing Home Screen [User Portal]', () => { test('Info toast should be visible when user tries to create a post with a valid body', async () => { const getOrganizationIdSpy = jest .spyOn(getOrganizationId, 'default') - .mockImplementation(() => { - return ''; - }); + .mockReturnValue(''); - render( - - - - - - - - - - ); + renderComponent(); await wait(); @@ -275,11 +230,28 @@ describe('Testing Home Screen [User Portal]', () => { const randomPostInput = 'This is a test'; userEvent.type(screen.getByTestId('postInput'), randomPostInput); - expect(screen.queryByText(randomPostInput)).toBeInTheDocument(); + + expect(screen.getByText(randomPostInput)).toBeInTheDocument(); userEvent.click(screen.getByTestId('postAction')); - expect(toast.error).not.toBeCalledWith(); - expect(toast.info).toBeCalledWith('Processing your post. Please wait.'); + expect(toast.error).not.toHaveBeenCalled(); + expect(toast.info).toHaveBeenCalledWith( + 'Processing your post. Please wait.' + ); }); + + function renderComponent(): void { + render( + + + + + + + + + + ); + } });