mattermost-mobile/app/screens/terms_of_service/terms_of_service.test.js
Chetanya Kandhari 68eff30eda [#MM-12410] Custom Terms of Service (#2234)
* [#MM-12410] Custom Terms of Service Modal.

- Create a new screen for terms of service
- Register it in app navigation
- Open it as a modal when login succeeds or app is opened when already logged in.
- Get terms of service and render it as markdown

* Refactor service terms to terms of service

* Show terms modal when launching channel instead of componentDidMount

* Remove footer text

* Close modal before logging out

* Use dismissAllModals instead od this.close

* Update header styles

* Update the back icon

* Add left icon conditinally based on os

* Update en.json

* - Integrate Accept/Reject API
- Refactor show navigator to util
- Remove title
- Code refactoring

* Handle Accept/Reject API errors

* Update package-lock

* - Show Alert to handle API error
- Handle API error for GetTerms error, Accept/Reject error and Reject success
- Changes in en.json
- Remove error banner
- Open terms modal in sso and experimental certificate

* Enable navigator buttons after getting terms

* Rename Ok to OK in Alert

* Add retry for API error

* Fix check-style

* Fix FailedNetworkAction Error Message

* - Use Close button instead of back button
- Update en.json

* Remove unused import

* Disable hashtags and atmentions in terms modal

* Enable close button when terms text fails to load

* Minor changes

* Fix show terms on sso login

* Handle restartApp event

* Add test cases

* Revert change to app/components/formatted_markdown_text.js

* Show the terms modal on the channel page

* Remove remaining references to showTermsOfService

* Disable Hashtags, AtMentions and ChannelLinks

* Update the props to markdown component

* Pass the text as literal to renderText

* Return the generated renderTexts

* Fix lint

* Render ~, #, @ with Channel link, Hashtags and At Mentions

* Fix failing tests
Revert unwanted changes

* Hide the channel page content before the terms are accepted

* Fix lint and failing tests

* Revert changes to hide channel while terms are not accepted

* Open modal in componentDidMount instead of constructor
Code refactoring
Disable back button on android

* Remove unused import

* No need to await for showModal

* No need to await for accept and reject

* Update flag for updateTermsOfServiceStatus to updateMyTermsOfServiceStatus
- as phase 2 redux PR was mered.

* Fix check-style

* Change opacity in terms of service text

- Fix tests

* Revert package-lock.json changes
2018-11-14 11:36:45 -03:00

134 lines
4.3 KiB
JavaScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react';
import {shallow} from 'enzyme';
import Preferences from 'mattermost-redux/constants/preferences';
import TermsOfService from './terms_of_service.js';
jest.mock('react-intl');
jest.mock('app/utils/theme', () => {
const original = require.requireActual('app/utils/theme');
return {
...original,
changeOpacity: jest.fn(),
};
});
describe('TermsOfService', () => {
const actions = {
getTermsOfService: jest.fn(),
updateMyTermsOfServiceStatus: jest.fn(),
logout: jest.fn(),
};
const baseProps = {
actions,
navigator: {
dismissAllModals: jest.fn(),
dismissModal: jest.fn(),
setButtons: jest.fn(),
setOnNavigatorEvent: jest.fn(),
},
theme: Preferences.THEMES.default,
closeButton: {},
siteName: 'Mattermost',
};
test('should match snapshot', () => {
const wrapper = shallow(
<TermsOfService {...baseProps}/>,
{context: {intl: {formatMessage: jest.fn()}}},
);
expect(wrapper.getElement()).toMatchSnapshot();
});
test('should match snapshot for get terms', async () => {
const wrapper = shallow(
<TermsOfService {...baseProps}/>,
{context: {intl: {formatMessage: jest.fn()}}},
);
await actions.getTermsOfService();
wrapper.update();
expect(wrapper.getElement()).toMatchSnapshot();
});
test('should match snapshot for fail of get terms', async () => {
const getTermsOfService = async () => {
return {
error: {},
};
};
const props = {
...baseProps,
actions: {
...actions,
getTermsOfService,
},
};
const wrapper = shallow(
<TermsOfService {...props}/>,
{context: {intl: {formatMessage: jest.fn()}}},
);
expect(wrapper.state('loading')).toEqual(true);
await getTermsOfService();
expect(wrapper.state('loading')).toEqual(false);
wrapper.update();
expect(wrapper.getElement()).toMatchSnapshot();
});
test('should call props.navigator.setButtons on setNavigatorButtons', async () => {
const wrapper = shallow(
<TermsOfService {...baseProps}/>,
{context: {intl: {formatMessage: jest.fn()}}},
);
wrapper.setState({loading: false, termsId: 1, termsText: 'Terms Text'});
expect(baseProps.navigator.setButtons).toHaveBeenCalledTimes(2);
wrapper.instance().setNavigatorButtons(true);
expect(baseProps.navigator.setButtons).toHaveBeenCalledTimes(3);
wrapper.instance().setNavigatorButtons(false);
expect(baseProps.navigator.setButtons).toHaveBeenCalledTimes(4);
});
test('should enable/disable navigator buttons on setNavigatorButtons true/false', () => {
const wrapper = shallow(
<TermsOfService {...baseProps}/>,
{context: {intl: {formatMessage: jest.fn()}}},
);
wrapper.setState({loading: false, termsId: 1, termsText: 'Terms Text'});
wrapper.instance().setNavigatorButtons(true);
expect(wrapper.getElement()).toMatchSnapshot();
wrapper.instance().setNavigatorButtons(false);
expect(wrapper.getElement()).toMatchSnapshot();
});
test('should match snapshot on enableNavigatorLogout', () => {
const wrapper = shallow(
<TermsOfService {...baseProps}/>,
{context: {intl: {formatMessage: jest.fn()}}},
);
wrapper.setState({loading: false, termsId: 1, termsText: 'Terms Text'});
wrapper.instance().enableNavigatorLogout();
expect(wrapper.getElement()).toMatchSnapshot();
});
test('should call dismissAllModals on closeTermsAndLogout', () => {
const wrapper = shallow(
<TermsOfService {...baseProps}/>,
{context: {intl: {formatMessage: jest.fn()}}},
);
wrapper.setState({loading: false, termsId: 1, termsText: 'Terms Text'});
wrapper.instance().closeTermsAndLogout();
expect(baseProps.navigator.dismissAllModals).toHaveBeenCalledTimes(1);
});
});