Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
machadoum committed May 20, 2022
1 parent 52d220c commit fe7ea93
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,15 @@ jest.mock('react-router-dom', () => {
};
});

const mockedUseIsGroupedNavigationEnabled = jest.fn();
jest.mock('../navigation/helpers', () => ({
useIsGroupedNavigationEnabled: () => mockedUseIsGroupedNavigationEnabled(),
}));

describe('UrlStateContainer', () => {
afterEach(() => {
jest.clearAllMocks();
mockedUseIsGroupedNavigationEnabled.mockReturnValue(false);
});
describe('handleInitialize', () => {
describe('URL state updates redux', () => {
Expand Down Expand Up @@ -226,6 +232,44 @@ describe('UrlStateContainer', () => {
expect(mockHistory.replace).not.toHaveBeenCalled();
});

it("it doesn't update URL state when on admin page and grouped nav disabled", () => {
mockedUseIsGroupedNavigationEnabled.mockReturnValue(false);
mockProps = getMockPropsObj({
page: CONSTANTS.unknown,
examplePath: '/administration',
namespaceLower: 'administration',
pageName: SecurityPageName.administration,
detailName: undefined,
}).noSearch.undefinedQuery;

(useLocation as jest.Mock).mockReturnValue({
pathname: mockProps.pathName,
});

mount(<HookWrapper hookProps={mockProps} hook={(args) => useUrlStateHooks(args)} />);

expect(mockHistory.replace.mock.calls[0][0].search).toBe('?');
});

it("it doesn't update URL state when on admin page and grouped nav enabled", () => {
mockedUseIsGroupedNavigationEnabled.mockReturnValue(true);
mockProps = getMockPropsObj({
page: CONSTANTS.unknown,
examplePath: '/dashboards',
namespaceLower: 'dashboards',
pageName: SecurityPageName.dashboardsLanding,
detailName: undefined,
}).noSearch.undefinedQuery;

(useLocation as jest.Mock).mockReturnValue({
pathname: mockProps.pathName,
});

mount(<HookWrapper hookProps={mockProps} hook={(args) => useUrlStateHooks(args)} />);

expect(mockHistory.replace.mock.calls[0][0].search).toBe('?');
});

it('it removes empty AppQuery state from URL', () => {
mockProps = {
...getMockProps(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { getFilterQuery, getMockPropsObj, mockHistory, testCases } from './test_
import { UrlStateContainerPropTypes } from './types';
import { useUrlStateHooks } from './use_url_state';
import { useLocation } from 'react-router-dom';
import { MANAGEMENT_PATH } from '../../../../common/constants';
import { DASHBOARDS_PATH, MANAGEMENT_PATH } from '../../../../common/constants';

let mockProps: UrlStateContainerPropTypes;

Expand Down Expand Up @@ -45,7 +45,16 @@ jest.mock('react-redux', () => {
};
});

const mockedUseIsGroupedNavigationEnabled = jest.fn();
jest.mock('../navigation/helpers', () => ({
useIsGroupedNavigationEnabled: () => mockedUseIsGroupedNavigationEnabled(),
}));

describe('UrlStateContainer - lodash.throttle mocked to test update url', () => {
beforeAll(() => {
mockedUseIsGroupedNavigationEnabled.mockReturnValue(false);
});

afterEach(() => {
jest.clearAllMocks();
jest.resetAllMocks();
Expand Down Expand Up @@ -210,7 +219,8 @@ describe('UrlStateContainer - lodash.throttle mocked to test update url', () =>
});
});

test("administration page doesn't has query string", () => {
test("administration page doesn't has query string when grouped nav disabled", () => {
mockedUseIsGroupedNavigationEnabled.mockReturnValue(false);
mockProps = getMockPropsObj({
page: CONSTANTS.networkPage,
examplePath: '/network',
Expand Down Expand Up @@ -285,6 +295,83 @@ describe('UrlStateContainer - lodash.throttle mocked to test update url', () =>
state: '',
});
});

test("administration page doesn't has query string when grouped nav enabled", () => {
mockedUseIsGroupedNavigationEnabled.mockReturnValue(true);
mockProps = getMockPropsObj({
page: CONSTANTS.networkPage,
examplePath: '/network',
namespaceLower: 'network',
pageName: SecurityPageName.network,
detailName: undefined,
}).noSearch.definedQuery;

const urlState = {
...mockProps.urlState,
[CONSTANTS.appQuery]: getFilterQuery(),
[CONSTANTS.timerange]: {
global: {
[CONSTANTS.timerange]: {
from: '2020-07-07T08:20:18.966Z',
fromStr: 'now-24h',
kind: 'relative',
to: '2020-07-08T08:20:18.966Z',
toStr: 'now',
},
linkTo: ['timeline'],
},
timeline: {
[CONSTANTS.timerange]: {
from: '2020-07-07T08:20:18.966Z',
fromStr: 'now-24h',
kind: 'relative',
to: '2020-07-08T08:20:18.966Z',
toStr: 'now',
},
linkTo: ['global'],
},
},
};

const updatedMockProps = {
...getMockPropsObj({
...mockProps,
page: CONSTANTS.unknown,
examplePath: DASHBOARDS_PATH,
namespaceLower: 'dashboards',
pageName: SecurityPageName.dashboardsLanding,
detailName: undefined,
}).noSearch.definedQuery,
urlState,
};

(useLocation as jest.Mock).mockReturnValue({
pathname: mockProps.pathName,
});

const wrapper = mount(
<HookWrapper
hookProps={{ ...mockProps, urlState }}
hook={(args) => useUrlStateHooks(args)}
/>
);

(useLocation as jest.Mock).mockReturnValue({
pathname: updatedMockProps.pathName,
});

wrapper.setProps({
hookProps: updatedMockProps,
});

wrapper.update();
expect(mockHistory.replace.mock.calls[1][0]).toStrictEqual({
hash: '',
pathname: DASHBOARDS_PATH,
search: '?',
state: '',
});
});
});

describe('handleInitialize', () => {
Expand Down

0 comments on commit fe7ea93

Please sign in to comment.