2020-05-21 23:38:47 +05:30
|
|
|
import React from 'react';
|
2017-06-08 01:39:11 +05:30
|
|
|
import sinon from 'sinon';
|
2019-12-08 00:32:00 +05:30
|
|
|
import expect from 'app/test/unexpected';
|
2020-05-21 23:38:47 +05:30
|
|
|
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', () => {
|
2019-11-27 14:33:32 +05:30
|
|
|
describe('#onRuleClick()', () => {
|
|
|
|
const id = 'rule-1-2';
|
|
|
|
const pathname = '/foo';
|
|
|
|
const search = '?bar';
|
2020-05-21 23:38:47 +05:30
|
|
|
let page: HTMLElement;
|
2020-01-18 02:07:52 +05:30
|
|
|
let replace: Function;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
replace = sinon.stub().named('history.replace');
|
|
|
|
|
2020-05-21 23:38:47 +05:30
|
|
|
({ container: page } = render(
|
|
|
|
<TestContextProvider>
|
|
|
|
<RulesPage
|
|
|
|
location={{ pathname, search } as any}
|
|
|
|
history={{ replace }}
|
|
|
|
/>
|
|
|
|
</TestContextProvider>,
|
|
|
|
));
|
2017-06-08 01:39:11 +05:30
|
|
|
});
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
it('should update location on rule click', () => {
|
|
|
|
const expectedUrl = `/foo?bar#${id}`;
|
|
|
|
|
2020-05-21 23:38:47 +05:30
|
|
|
fireEvent.click(page.querySelector(`#${id}`) as HTMLElement);
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
expect(replace, 'to have a call satisfying', [expectedUrl]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not update location if link was clicked', () => {
|
2020-05-21 23:38:47 +05:30
|
|
|
fireEvent.click(screen.getByText('/register', { exact: false }));
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
expect(replace, 'was not called');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not update location if defaultPrevented', () => {
|
2020-05-21 23:38:47 +05:30
|
|
|
const el = page.querySelector(`#${id}`) as HTMLElement;
|
|
|
|
const event = createEvent.click(el);
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-21 23:38:47 +05:30
|
|
|
event.preventDefault();
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-21 23:38:47 +05:30
|
|
|
fireEvent(el, event);
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
expect(replace, 'was not called');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not update location if no id', () => {
|
2020-05-21 23:38:47 +05:30
|
|
|
const el = page.querySelector(`#${id}`) as HTMLElement;
|
|
|
|
|
|
|
|
el.id = '';
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-21 23:38:47 +05:30
|
|
|
fireEvent.click(el);
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
expect(replace, 'was not called');
|
|
|
|
});
|
|
|
|
});
|
2017-06-08 01:39:11 +05:30
|
|
|
});
|