-
-
Notifications
You must be signed in to change notification settings - Fork 757
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add chat screen tests and fix other bugs
- Loading branch information
1 parent
46c6e45
commit 062d3f9
Showing
5 changed files
with
291 additions
and
4 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
116 changes: 116 additions & 0 deletions
116
src/components/UserPortal/ContactCard/ContactCard.test.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,116 @@ | ||
import React from 'react'; | ||
import { act, render, screen } from '@testing-library/react'; | ||
import { MockedProvider } from '@apollo/react-testing'; | ||
import { I18nextProvider } from 'react-i18next'; | ||
|
||
import { BrowserRouter } from 'react-router-dom'; | ||
import { Provider } from 'react-redux'; | ||
import { store } from 'state/store'; | ||
import i18nForTest from 'utils/i18nForTest'; | ||
import { StaticMockLink } from 'utils/StaticMockLink'; | ||
import ContactCard from './ContactCard'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
const link = new StaticMockLink([], true); | ||
|
||
async function wait(ms = 100): Promise<void> { | ||
await act(() => { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, ms); | ||
}); | ||
}); | ||
} | ||
|
||
let props = { | ||
id: '1', | ||
firstName: 'Noble', | ||
lastName: 'Mittal', | ||
email: '[email protected]', | ||
image: '', | ||
selectedContact: '', | ||
setSelectedContact: jest.fn(), | ||
setSelectedContactName: jest.fn(), | ||
}; | ||
|
||
describe('Testing ContactCard Component [User Portal]', () => { | ||
test('Component should be rendered properly if person image is undefined', async () => { | ||
render( | ||
<MockedProvider addTypename={false} link={link}> | ||
<BrowserRouter> | ||
<Provider store={store}> | ||
<I18nextProvider i18n={i18nForTest}> | ||
<ContactCard {...props} /> | ||
</I18nextProvider> | ||
</Provider> | ||
</BrowserRouter> | ||
</MockedProvider> | ||
); | ||
|
||
await wait(); | ||
}); | ||
|
||
test('Component should be rendered properly if person image is not undefined', async () => { | ||
props = { | ||
...props, | ||
image: 'personImage', | ||
}; | ||
|
||
render( | ||
<MockedProvider addTypename={false} link={link}> | ||
<BrowserRouter> | ||
<Provider store={store}> | ||
<I18nextProvider i18n={i18nForTest}> | ||
<ContactCard {...props} /> | ||
</I18nextProvider> | ||
</Provider> | ||
</BrowserRouter> | ||
</MockedProvider> | ||
); | ||
|
||
await wait(); | ||
}); | ||
|
||
test('Contact gets selectected when component is clicked', async () => { | ||
render( | ||
<MockedProvider addTypename={false} link={link}> | ||
<BrowserRouter> | ||
<Provider store={store}> | ||
<I18nextProvider i18n={i18nForTest}> | ||
<ContactCard {...props} /> | ||
</I18nextProvider> | ||
</Provider> | ||
</BrowserRouter> | ||
</MockedProvider> | ||
); | ||
|
||
await wait(); | ||
|
||
userEvent.click(screen.getByTestId('contactContainer')); | ||
|
||
await wait(); | ||
}); | ||
|
||
test('Component is rendered with background color grey if the contact is selected', async () => { | ||
props = { | ||
...props, | ||
selectedContact: '1', | ||
}; | ||
render( | ||
<MockedProvider addTypename={false} link={link}> | ||
<BrowserRouter> | ||
<Provider store={store}> | ||
<I18nextProvider i18n={i18nForTest}> | ||
<ContactCard {...props} /> | ||
</I18nextProvider> | ||
</Provider> | ||
</BrowserRouter> | ||
</MockedProvider> | ||
); | ||
|
||
await wait(); | ||
|
||
userEvent.click(screen.getByTestId('contactContainer')); | ||
|
||
await wait(); | ||
}); | ||
}); |
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,173 @@ | ||
import React from 'react'; | ||
import { act, render, screen } from '@testing-library/react'; | ||
import { MockedProvider } from '@apollo/react-testing'; | ||
import { I18nextProvider } from 'react-i18next'; | ||
|
||
import { ORGANIZATIONS_MEMBER_CONNECTION_LIST } from 'GraphQl/Queries/Queries'; | ||
import { BrowserRouter } from 'react-router-dom'; | ||
import { Provider } from 'react-redux'; | ||
import { store } from 'state/store'; | ||
import i18nForTest from 'utils/i18nForTest'; | ||
import { StaticMockLink } from 'utils/StaticMockLink'; | ||
import Chat from './Chat'; | ||
import * as getOrganizationId from 'utils/getOrganizationId'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
const MOCKS = [ | ||
{ | ||
request: { | ||
query: ORGANIZATIONS_MEMBER_CONNECTION_LIST, | ||
variables: { | ||
orgId: '', | ||
firstName_contains: '', | ||
}, | ||
}, | ||
result: { | ||
data: { | ||
organizationsMemberConnection: { | ||
edges: [ | ||
{ | ||
_id: '64001660a711c62d5b4076a2', | ||
firstName: 'Noble', | ||
lastName: 'Mittal', | ||
image: null, | ||
email: '[email protected]', | ||
createdAt: '2023-03-02T03:22:08.101Z', | ||
}, | ||
{ | ||
_id: '64001660a711c62d5b4076a3', | ||
firstName: 'Noble', | ||
lastName: 'Mittal', | ||
image: 'mockImage', | ||
email: '[email protected]', | ||
createdAt: '2023-03-02T03:22:08.101Z', | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
request: { | ||
query: ORGANIZATIONS_MEMBER_CONNECTION_LIST, | ||
variables: { | ||
orgId: '', | ||
firstName_contains: 'j', | ||
}, | ||
}, | ||
result: { | ||
data: { | ||
organizationsMemberConnection: { | ||
edges: [ | ||
{ | ||
_id: '64001660a711c62d5b4076a2', | ||
firstName: 'John', | ||
lastName: 'Cena', | ||
image: null, | ||
email: '[email protected]', | ||
createdAt: '2023-03-02T03:22:08.101Z', | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}, | ||
]; | ||
|
||
const link = new StaticMockLink(MOCKS, true); | ||
|
||
async function wait(ms = 100): Promise<void> { | ||
await act(() => { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, ms); | ||
}); | ||
}); | ||
} | ||
|
||
describe('Testing People 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(), | ||
})), | ||
}); | ||
|
||
const getOrganizationIdSpy = jest | ||
.spyOn(getOrganizationId, 'default') | ||
.mockImplementation(() => { | ||
return ''; | ||
}); | ||
|
||
test('Screen should be rendered properly', async () => { | ||
render( | ||
<MockedProvider addTypename={false} link={link}> | ||
<BrowserRouter> | ||
<Provider store={store}> | ||
<I18nextProvider i18n={i18nForTest}> | ||
<Chat /> | ||
</I18nextProvider> | ||
</Provider> | ||
</BrowserRouter> | ||
</MockedProvider> | ||
); | ||
|
||
await wait(); | ||
|
||
expect(getOrganizationIdSpy).toHaveBeenCalled(); | ||
expect(screen.queryAllByText('Noble Mittal')).not.toBe([]); | ||
}); | ||
|
||
test('User is able to select a contact', async () => { | ||
render( | ||
<MockedProvider addTypename={false} link={link}> | ||
<BrowserRouter> | ||
<Provider store={store}> | ||
<I18nextProvider i18n={i18nForTest}> | ||
<Chat /> | ||
</I18nextProvider> | ||
</Provider> | ||
</BrowserRouter> | ||
</MockedProvider> | ||
); | ||
|
||
await wait(); | ||
|
||
userEvent.click(screen.getByText('[email protected]')); | ||
await wait(); | ||
|
||
expect(getOrganizationIdSpy).toHaveBeenCalled(); | ||
expect(screen.queryAllByText('Noble Mittal')).not.toBe([]); | ||
}); | ||
|
||
test('Search functionality works as expected', async () => { | ||
render( | ||
<MockedProvider addTypename={false} link={link}> | ||
<BrowserRouter> | ||
<Provider store={store}> | ||
<I18nextProvider i18n={i18nForTest}> | ||
<Chat /> | ||
</I18nextProvider> | ||
</Provider> | ||
</BrowserRouter> | ||
</MockedProvider> | ||
); | ||
|
||
await wait(); | ||
|
||
userEvent.type(screen.getByTestId('searchInput'), 'j'); | ||
await wait(); | ||
|
||
expect(getOrganizationIdSpy).toHaveBeenCalled(); | ||
expect(screen.queryByText('John Cena')).toBeInTheDocument(); | ||
expect(screen.queryByText('Noble Mittal')).not.toBeInTheDocument(); | ||
}); | ||
}); |
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