-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1461 from glific/enhamncement/template-variables
added contact variables suggestion
- Loading branch information
Showing
5 changed files
with
166 additions
and
26 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
71 changes: 61 additions & 10 deletions
71
src/containers/Chat/ChatMessages/AddVariables/AddVariables.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 |
---|---|---|
@@ -1,29 +1,80 @@ | ||
import { fireEvent, render, waitFor } from '@testing-library/react'; | ||
import { act, fireEvent, render, screen, within } from '@testing-library/react'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
import { MemoryRouter } from 'react-router-dom'; | ||
import { MockedProvider } from '@apollo/client/testing'; | ||
import axios from 'axios'; | ||
|
||
import { AddVariables } from './AddVariables'; | ||
import { FLOW_EDITOR_API } from '../../../../config'; | ||
import { responseData, responseData1 } from '../../../../mocks/AddVariables'; | ||
|
||
jest.mock('axios', () => { | ||
return { | ||
get: jest.fn(), | ||
}; | ||
}); | ||
|
||
const setVariableMock = jest.fn(); | ||
const mocks = [FLOW_EDITOR_API]; | ||
|
||
const defaultProps = { | ||
setVariable: setVariableMock, | ||
handleCancel: jest.fn(), | ||
bodyText: 'Your OTP for {{1}} is {{2}}. This is valid for {{3}}.', | ||
bodyText: 'Hi {{1}}, Please find the attached bill.', | ||
updateEditorState: jest.fn(), | ||
variableParams: jest.fn(), | ||
variableParam: ['this', '4563', '5 minutes'], | ||
}; | ||
|
||
test('it should render', () => { | ||
const { getByTestId } = render(<AddVariables {...defaultProps} />); | ||
const wrapper = ( | ||
<MockedProvider> | ||
<MemoryRouter> | ||
<AddVariables {...defaultProps} mocks={mocks} /> | ||
</MemoryRouter> | ||
</MockedProvider> | ||
); | ||
|
||
const whenStable = async () => { | ||
await act(async () => { | ||
await new Promise((resolve) => setTimeout(resolve, 0)); | ||
}); | ||
}; | ||
|
||
const axiosApiCall = async () => { | ||
axios.get.mockImplementationOnce(() => Promise.resolve(responseData1)); | ||
|
||
axios.get.mockImplementationOnce(() => Promise.resolve(responseData)); | ||
}; | ||
|
||
test('it should render variable options and save the form', async () => { | ||
axiosApiCall(); | ||
const { container, getByTestId, getByText } = render(wrapper); | ||
|
||
const getSpy = jest.spyOn(axios, 'get').mockResolvedValueOnce(responseData); | ||
await whenStable(); | ||
|
||
expect(getByTestId('variablesDialog')).toBeInTheDocument(); | ||
}); | ||
const autocomplete = screen.getByTestId('AutocompleteInput'); | ||
const input = within(autocomplete).getByRole('textbox'); | ||
|
||
test('save form', async () => { | ||
const { getByText } = render(<AddVariables {...defaultProps} />); | ||
autocomplete.focus(); | ||
// assign value to input field | ||
fireEvent.change(input, { target: { value: '@contact.name' } }); | ||
// navigate to the first item in the autocomplete box | ||
fireEvent.keyDown(autocomplete, { key: 'ArrowDown' }); | ||
// select the first item | ||
fireEvent.keyDown(autocomplete, { key: 'Enter' }); | ||
|
||
fireEvent.click(getByText('Done')); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(setVariableMock).toHaveBeenCalled(); | ||
}); | ||
test('cancel button clicked', async () => { | ||
axiosApiCall(); | ||
const { container, getByTestId, getByText } = render(wrapper); | ||
|
||
const getSpy = jest.spyOn(axios, 'get').mockResolvedValueOnce(responseData); | ||
await whenStable(); | ||
|
||
expect(getByTestId('variablesDialog')).toBeInTheDocument(); | ||
fireEvent.click(getByText('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
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,37 @@ | ||
export const responseData1 = { | ||
data: { | ||
results: [ | ||
{ key: 'settings', label: 'Settings', name: 'Settings', value_type: 'text' }, | ||
{ key: 'dob', label: 'Date of Birth', name: 'Date of Birth', value_type: 'text' }, | ||
], | ||
}, | ||
}; | ||
export const responseData = { | ||
data: { | ||
types: [ | ||
{ key_source: 'flow', name: 'flow', property_template: Array(3) }, | ||
{ key_source: 'fields', name: 'fields' }, | ||
{ key_source: 'results', name: 'results' }, | ||
{ name: 'urns', properties: Array(1) }, | ||
{ name: 'channel', properties: Array(4) }, | ||
{ | ||
name: 'contact', | ||
properties: [ | ||
{ help: 'the name or URN', key: '__default__', type: 'text' }, | ||
{ help: 'the numeric ID of the contact', key: 'id', type: 'text' }, | ||
{ help: 'the name of the contact', key: 'name', type: 'text' }, | ||
{ | ||
help: 'the language of the contact as 3-letter ISO code', | ||
key: 'language', | ||
type: 'text', | ||
}, | ||
{ | ||
help: 'the gender of the contact like male/female/others', | ||
key: 'gender', | ||
type: 'text', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
}; |