accounts-frontend/packages/app/pages/rules/RulesPage.test.tsx

63 lines
1.8 KiB
TypeScript
Raw Normal View History

import React from 'react';
2017-06-08 01:39:11 +05:30
import sinon from 'sinon';
import expect from 'app/test/unexpected';
import { render, fireEvent, createEvent, screen } from '@testing-library/react';
import { TestContextProvider } from 'app/shell';
2017-06-08 01:39:11 +05:30
import RulesPage from './RulesPage';
describe('RulesPage', () => {
2020-05-24 04:38:24 +05:30
describe('#onRuleClick()', () => {
const id = 'rule-1-2';
const pathname = '/foo';
const search = '?bar';
let page: HTMLElement;
let replace: Function;
2020-05-24 04:38:24 +05:30
beforeEach(() => {
replace = sinon.stub().named('history.replace');
2020-05-24 04:38:24 +05:30
({ container: page } = render(
<TestContextProvider>
<RulesPage location={{ pathname, search } as any} history={{ replace }} />
</TestContextProvider>,
));
});
2020-05-24 04:38:24 +05:30
it('should update location on rule click', () => {
const expectedUrl = `/foo?bar#${id}`;
2020-05-24 04:38:24 +05:30
fireEvent.click(page.querySelector(`#${id}`) as HTMLElement);
2020-05-24 04:38:24 +05:30
expect(replace, 'to have a call satisfying', [expectedUrl]);
});
2020-05-24 04:38:24 +05:30
it('should not update location if link was clicked', () => {
fireEvent.click(screen.getByText('/register', { exact: false }));
2020-05-24 04:38:24 +05:30
expect(replace, 'was not called');
});
2020-05-24 04:38:24 +05:30
it('should not update location if defaultPrevented', () => {
const el = page.querySelector(`#${id}`) as HTMLElement;
const event = createEvent.click(el);
2020-05-24 04:38:24 +05:30
event.preventDefault();
fireEvent(el, event);
expect(replace, 'was not called');
});
2020-05-24 04:38:24 +05:30
it('should not update location if no id', () => {
const el = page.querySelector(`#${id}`) as HTMLElement;
2020-05-24 04:38:24 +05:30
el.id = '';
2020-05-24 04:38:24 +05:30
fireEvent.click(el);
2020-05-24 04:38:24 +05:30
expect(replace, 'was not called');
});
});
2017-06-08 01:39:11 +05:30
});