Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated and optimized Home.test.tsx #1147

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 48 additions & 76 deletions src/screens/UserPortal/Home/Home.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,40 +151,31 @@ async function wait(ms = 100): Promise<void> {
}

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(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<Home />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>
);
renderComponent();

await wait();

Expand All @@ -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(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<Home />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>
);
renderComponent();

await wait();

Expand All @@ -217,69 +196,62 @@ 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(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<Home />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>
);
renderComponent();

await wait();

expect(getOrganizationIdSpy).toHaveBeenCalled();

userEvent.click(screen.getByTestId('postAction'));

expect(toast.error).toBeCalledWith(
expect(toast.error).toHaveBeenCalledWith(
"Can't create a post with an empty body."
);
});

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(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<Home />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>
);
renderComponent();

await wait();

expect(getOrganizationIdSpy).toHaveBeenCalled();

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(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<Home />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>
);
}
});
Loading