Commit 1e4d2527 authored by Paul Slaughter's avatar Paul Slaughter

Merge branch '336917-reset-location-before-each-test' into 'master'

Reset window location before every test [RUN AS-IF-FOSS]

See merge request gitlab-org/gitlab!67353
parents 124da723 e4e05a45
...@@ -425,6 +425,10 @@ it('does something', () => { ...@@ -425,6 +425,10 @@ it('does something', () => {
### Mocking the current location in Jest ### Mocking the current location in Jest
NOTE:
The value of `window.location.href` is reset before every test to avoid earlier
tests affecting later ones.
If your tests require `window.location.href` to take a particular value, use If your tests require `window.location.href` to take a particular value, use
the `setWindowLocation` helper: the `setWindowLocation` helper:
...@@ -442,7 +446,6 @@ it('passes', () => { ...@@ -442,7 +446,6 @@ it('passes', () => {
}); });
``` ```
NOTE:
To modify only the hash, use either the `setWindowLocation` helper, or assign To modify only the hash, use either the `setWindowLocation` helper, or assign
directly to `window.location.hash`, e.g.: directly to `window.location.hash`, e.g.:
......
...@@ -610,8 +610,8 @@ describe('OnDemandScansForm', () => { ...@@ -610,8 +610,8 @@ describe('OnDemandScansForm', () => {
createShallowComponent(); createShallowComponent();
await wrapper.vm.$nextTick(); await wrapper.vm.$nextTick();
expect(findScannerProfilesSelector().attributes('value')).toBe(scannerProfile.id); expect(findScannerProfilesSelector().attributes('value')).toBe(dastScan.scannerProfileId);
expect(findSiteProfilesSelector().attributes('value')).toBe(siteProfile.id); expect(findSiteProfilesSelector().attributes('value')).toBe(dastScan.siteProfileId);
}); });
}); });
......
...@@ -24,6 +24,7 @@ import { mockTracking, unmockTracking } from 'helpers/tracking_helper'; ...@@ -24,6 +24,7 @@ import { mockTracking, unmockTracking } from 'helpers/tracking_helper';
import { extendedWrapper } from 'helpers/vue_test_utils_helper'; import { extendedWrapper } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises'; import waitForPromises from 'helpers/wait_for_promises';
import createFlash from '~/flash'; import createFlash from '~/flash';
import { queryToObject } from '~/lib/utils/url_utility';
import FilteredSearchBarRoot from '~/vue_shared/components/filtered_search_bar/filtered_search_bar_root.vue'; import FilteredSearchBarRoot from '~/vue_shared/components/filtered_search_bar/filtered_search_bar_root.vue';
import { import {
...@@ -904,9 +905,12 @@ describe('RequirementsRoot', () => { ...@@ -904,9 +905,12 @@ describe('RequirementsRoot', () => {
expect(wrapper.vm.prevPageCursor).toBe(''); expect(wrapper.vm.prevPageCursor).toBe('');
expect(wrapper.vm.nextPageCursor).toBe(mockPageInfo.endCursor); expect(wrapper.vm.nextPageCursor).toBe(mockPageInfo.endCursor);
expect(global.window.location.href).toBe( expect(queryToObject(window.location.search)).toEqual({
`${TEST_HOST}/?page=2&state=opened&sort=created_desc&next=${mockPageInfo.endCursor}`, page: '2',
); state: 'opened',
sort: 'created_desc',
next: mockPageInfo.endCursor,
});
expect(trackingSpy).toHaveBeenCalledWith(undefined, 'click_navigation', { expect(trackingSpy).toHaveBeenCalledWith(undefined, 'click_navigation', {
label: 'next', label: 'next',
}); });
...@@ -926,9 +930,12 @@ describe('RequirementsRoot', () => { ...@@ -926,9 +930,12 @@ describe('RequirementsRoot', () => {
expect(wrapper.vm.prevPageCursor).toBe(mockPageInfo.startCursor); expect(wrapper.vm.prevPageCursor).toBe(mockPageInfo.startCursor);
expect(wrapper.vm.nextPageCursor).toBe(''); expect(wrapper.vm.nextPageCursor).toBe('');
expect(global.window.location.href).toBe( expect(queryToObject(window.location.search)).toEqual({
`${TEST_HOST}/?page=1&state=opened&sort=created_desc&prev=${mockPageInfo.startCursor}`, page: '1',
); state: 'opened',
sort: 'created_desc',
prev: mockPageInfo.startCursor,
});
expect(trackingSpy).toHaveBeenCalledWith(undefined, 'click_navigation', { expect(trackingSpy).toHaveBeenCalledWith(undefined, 'click_navigation', {
label: 'prev', label: 'prev',
}); });
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
*/ */
const useMockLocation = (fn) => { const useMockLocation = (fn) => {
const origWindowLocation = window.location; const origWindowLocation = window.location;
let currentWindowLocation; let currentWindowLocation = origWindowLocation;
Object.defineProperty(window, 'location', { Object.defineProperty(window, 'location', {
get: () => currentWindowLocation, get: () => currentWindowLocation,
......
import setWindowLocation from './set_window_location_helper'; import setWindowLocation from './set_window_location_helper';
import { TEST_HOST } from './test_constants';
describe('helpers/set_window_location_helper', () => { describe('helpers/set_window_location_helper', () => {
const originalLocation = window.location.href; const originalLocation = window.location.href;
...@@ -103,4 +104,30 @@ describe('helpers/set_window_location_helper', () => { ...@@ -103,4 +104,30 @@ describe('helpers/set_window_location_helper', () => {
); );
}); });
}); });
// This set of tests relies on Jest executing tests in source order, which is
// at the time of writing the only order they will execute, by design.
// See https://github.com/facebook/jest/issues/4386 for more details.
describe('window.location resetting by global beforeEach', () => {
const overridden = 'https://gdk.test:1234/';
const initial = `${TEST_HOST}/`;
it('works before an override', () => {
expect(window.location.href).toBe(initial);
});
describe('overriding', () => {
beforeEach(() => {
setWindowLocation(overridden);
});
it('works', () => {
expect(window.location.href).toBe(overridden);
});
});
it('works after an override', () => {
expect(window.location.href).toBe(initial);
});
});
}); });
...@@ -259,6 +259,8 @@ describe('diffs/components/app', () => { ...@@ -259,6 +259,8 @@ describe('diffs/components/app', () => {
}); });
it('marks current diff file based on currently highlighted row', async () => { it('marks current diff file based on currently highlighted row', async () => {
window.location.hash = 'ABC_123';
createComponent({ createComponent({
shouldShow: true, shouldShow: true,
}); });
...@@ -429,9 +431,8 @@ describe('diffs/components/app', () => { ...@@ -429,9 +431,8 @@ describe('diffs/components/app', () => {
jest.spyOn(wrapper.vm, 'refetchDiffData').mockImplementation(() => {}); jest.spyOn(wrapper.vm, 'refetchDiffData').mockImplementation(() => {});
jest.spyOn(wrapper.vm, 'adjustView').mockImplementation(() => {}); jest.spyOn(wrapper.vm, 'adjustView').mockImplementation(() => {});
}; };
const location = window.location.href;
beforeAll(() => { beforeEach(() => {
setWindowLocation(COMMIT_URL); setWindowLocation(COMMIT_URL);
document.title = 'My Title'; document.title = 'My Title';
}); });
...@@ -440,10 +441,6 @@ describe('diffs/components/app', () => { ...@@ -440,10 +441,6 @@ describe('diffs/components/app', () => {
jest.spyOn(urlUtils, 'updateHistory'); jest.spyOn(urlUtils, 'updateHistory');
}); });
afterAll(() => {
setWindowLocation(location);
});
it('when the commit changes and the app is not loading it should update the history, refetch the diff data, and update the view', async () => { it('when the commit changes and the app is not loading it should update the history, refetch the diff data, and update the view', async () => {
createComponent({}, ({ state }) => { createComponent({}, ({ state }) => {
state.diffs.commit = { ...state.diffs.commit, id: 'OLD' }; state.diffs.commit = { ...state.diffs.commit, id: 'OLD' };
......
...@@ -448,7 +448,7 @@ describe('monitoring/utils', () => { ...@@ -448,7 +448,7 @@ describe('monitoring/utils', () => {
input | urlParams input | urlParams
${[]} | ${''} ${[]} | ${''}
${[{ name: 'env', value: 'prod' }]} | ${'?var-env=prod'} ${[{ name: 'env', value: 'prod' }]} | ${'?var-env=prod'}
${[{ name: 'env1', value: 'prod' }, { name: 'env2', value: null }]} | ${'?var-env=prod&var-env1=prod'} ${[{ name: 'env1', value: 'prod' }, { name: 'env2', value: null }]} | ${'?var-env1=prod'}
`( `(
'setCustomVariablesFromUrl updates history with query "$urlParams" with input $input', 'setCustomVariablesFromUrl updates history with query "$urlParams" with input $input',
({ input, urlParams }) => { ({ input, urlParams }) => {
......
...@@ -3,6 +3,8 @@ import * as jqueryMatchers from 'custom-jquery-matchers'; ...@@ -3,6 +3,8 @@ import * as jqueryMatchers from 'custom-jquery-matchers';
import Vue from 'vue'; import Vue from 'vue';
import 'jquery'; import 'jquery';
import { setGlobalDateToFakeDate } from 'helpers/fake_date'; import { setGlobalDateToFakeDate } from 'helpers/fake_date';
import setWindowLocation from 'helpers/set_window_location_helper';
import { TEST_HOST } from 'helpers/test_constants';
import Translate from '~/vue_shared/translate'; import Translate from '~/vue_shared/translate';
import { getJSONFixture, loadHTMLFixture, setHTMLFixture } from './__helpers__/fixtures'; import { getJSONFixture, loadHTMLFixture, setHTMLFixture } from './__helpers__/fixtures';
import { initializeTestTimeout } from './__helpers__/timeout'; import { initializeTestTimeout } from './__helpers__/timeout';
...@@ -88,8 +90,13 @@ Object.assign(global, { ...@@ -88,8 +90,13 @@ Object.assign(global, {
}, },
}); });
// make sure that each test actually tests something
// see https://jestjs.io/docs/en/expect#expecthasassertions
beforeEach(() => { beforeEach(() => {
// make sure that each test actually tests something
// see https://jestjs.io/docs/en/expect#expecthasassertions
expect.hasAssertions(); expect.hasAssertions();
// Reset the mocked window.location. This ensures tests don't interfere with
// each other, and removes the need to tidy up if it was changed for a given
// test.
setWindowLocation(TEST_HOST);
}); });
import { shallowMount } from '@vue/test-utils'; import { shallowMount } from '@vue/test-utils';
import setWindowLocation from 'helpers/set_window_location_helper';
import { historyPushState } from '~/lib/utils/common_utils'; import { historyPushState } from '~/lib/utils/common_utils';
import { mergeUrlParams } from '~/lib/utils/url_utility'; import { mergeUrlParams } from '~/lib/utils/url_utility';
import UrlSyncComponent from '~/vue_shared/components/url_sync.vue'; import UrlSyncComponent from '~/vue_shared/components/url_sync.vue';
...@@ -15,9 +14,6 @@ jest.mock('~/lib/utils/common_utils', () => ({ ...@@ -15,9 +14,6 @@ jest.mock('~/lib/utils/common_utils', () => ({
describe('url sync component', () => { describe('url sync component', () => {
let wrapper; let wrapper;
const mockQuery = { group_id: '5014437163714', project_ids: ['5014437608314'] }; const mockQuery = { group_id: '5014437163714', project_ids: ['5014437608314'] };
const TEST_HOST = 'http://testhost/';
setWindowLocation(TEST_HOST);
const findButton = () => wrapper.find('button'); const findButton = () => wrapper.find('button');
...@@ -35,7 +31,9 @@ describe('url sync component', () => { ...@@ -35,7 +31,9 @@ describe('url sync component', () => {
const expectUrlSync = (query, times, mergeUrlParamsReturnValue) => { const expectUrlSync = (query, times, mergeUrlParamsReturnValue) => {
expect(mergeUrlParams).toHaveBeenCalledTimes(times); expect(mergeUrlParams).toHaveBeenCalledTimes(times);
expect(mergeUrlParams).toHaveBeenCalledWith(query, TEST_HOST, { spreadArrays: true }); expect(mergeUrlParams).toHaveBeenCalledWith(query, window.location.href, {
spreadArrays: true,
});
expect(historyPushState).toHaveBeenCalledTimes(times); expect(historyPushState).toHaveBeenCalledTimes(times);
expect(historyPushState).toHaveBeenCalledWith(mergeUrlParamsReturnValue); expect(historyPushState).toHaveBeenCalledWith(mergeUrlParamsReturnValue);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment