diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-05-19 07:33:21 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-05-19 07:33:21 +0000 |
commit | 36a59d088eca61b834191dacea009677a96c052f (patch) | |
tree | e4f33972dab5d8ef79e3944a9f403035fceea43f /spec/frontend/__helpers__ | |
parent | a1761f15ec2cae7c7f7bbda39a75494add0dfd6f (diff) | |
download | gitlab-ce-15.0.0-rc42.tar.gz |
Add latest changes from gitlab-org/gitlab@15-0-stable-eev15.0.0-rc42
Diffstat (limited to 'spec/frontend/__helpers__')
-rw-r--r-- | spec/frontend/__helpers__/dom_shims/clipboard.js | 5 | ||||
-rw-r--r-- | spec/frontend/__helpers__/dom_shims/index.js | 1 | ||||
-rw-r--r-- | spec/frontend/__helpers__/fixtures.js | 15 | ||||
-rw-r--r-- | spec/frontend/__helpers__/flush_promises.js | 4 | ||||
-rw-r--r-- | spec/frontend/__helpers__/shared_test_setup.js | 7 | ||||
-rw-r--r-- | spec/frontend/__helpers__/user_mock_data_helper.js | 2 | ||||
-rw-r--r-- | spec/frontend/__helpers__/vuex_action_helper.js | 35 | ||||
-rw-r--r-- | spec/frontend/__helpers__/vuex_action_helper_spec.js | 61 | ||||
-rw-r--r-- | spec/frontend/__helpers__/wait_for_promises.js | 5 |
9 files changed, 39 insertions, 96 deletions
diff --git a/spec/frontend/__helpers__/dom_shims/clipboard.js b/spec/frontend/__helpers__/dom_shims/clipboard.js new file mode 100644 index 00000000000..3bc1b059272 --- /dev/null +++ b/spec/frontend/__helpers__/dom_shims/clipboard.js @@ -0,0 +1,5 @@ +Object.defineProperty(navigator, 'clipboard', { + value: { + writeText: () => {}, + }, +}); diff --git a/spec/frontend/__helpers__/dom_shims/index.js b/spec/frontend/__helpers__/dom_shims/index.js index 9b70cb86b8b..742d55196b4 100644 --- a/spec/frontend/__helpers__/dom_shims/index.js +++ b/spec/frontend/__helpers__/dom_shims/index.js @@ -1,3 +1,4 @@ +import './clipboard'; import './create_object_url'; import './element_scroll_into_view'; import './element_scroll_by'; diff --git a/spec/frontend/__helpers__/fixtures.js b/spec/frontend/__helpers__/fixtures.js index d8054d32fae..a6f7b37161e 100644 --- a/spec/frontend/__helpers__/fixtures.js +++ b/spec/frontend/__helpers__/fixtures.js @@ -20,24 +20,15 @@ Did you run bin/rake frontend:fixtures?`, return fs.readFileSync(absolutePath, 'utf8'); } -/** - * @deprecated Use `import` to load a JSON fixture instead. - * See https://docs.gitlab.com/ee/development/testing_guide/frontend_testing.html#use-fixtures, - * https://gitlab.com/gitlab-org/gitlab/-/issues/339346. - */ -export const getJSONFixture = (relativePath) => JSON.parse(getFixture(relativePath)); - export const resetHTMLFixture = () => { document.head.innerHTML = ''; document.body.innerHTML = ''; }; -export const setHTMLFixture = (htmlContent, resetHook = afterEach) => { +export const setHTMLFixture = (htmlContent) => { document.body.innerHTML = htmlContent; - resetHook(resetHTMLFixture); }; -export const loadHTMLFixture = (relativePath, resetHook = afterEach) => { - const fileContent = getFixture(relativePath); - setHTMLFixture(fileContent, resetHook); +export const loadHTMLFixture = (relativePath) => { + setHTMLFixture(getFixture(relativePath)); }; diff --git a/spec/frontend/__helpers__/flush_promises.js b/spec/frontend/__helpers__/flush_promises.js deleted file mode 100644 index eefc2ed7c17..00000000000 --- a/spec/frontend/__helpers__/flush_promises.js +++ /dev/null @@ -1,4 +0,0 @@ -export default function flushPromises() { - // eslint-disable-next-line no-restricted-syntax - return new Promise(setImmediate); -} diff --git a/spec/frontend/__helpers__/shared_test_setup.js b/spec/frontend/__helpers__/shared_test_setup.js index 7b5df18ee0f..011e1142c76 100644 --- a/spec/frontend/__helpers__/shared_test_setup.js +++ b/spec/frontend/__helpers__/shared_test_setup.js @@ -6,7 +6,6 @@ import 'jquery'; import Translate from '~/vue_shared/translate'; import setWindowLocation from './set_window_location_helper'; import { setGlobalDateToFakeDate } from './fake_date'; -import { loadHTMLFixture, setHTMLFixture } from './fixtures'; import { TEST_HOST } from './test_constants'; import * as customMatchers from './matchers'; @@ -28,12 +27,6 @@ Vue.config.productionTip = false; Vue.use(Translate); -// convenience wrapper for migration from Karma -Object.assign(global, { - loadFixtures: loadHTMLFixture, - setFixtures: setHTMLFixture, -}); - const JQUERY_MATCHERS_TO_EXCLUDE = ['toHaveLength', 'toExist']; // custom-jquery-matchers was written for an old Jest version, we need to make it compatible diff --git a/spec/frontend/__helpers__/user_mock_data_helper.js b/spec/frontend/__helpers__/user_mock_data_helper.js index db747283d9e..29ce95a88e2 100644 --- a/spec/frontend/__helpers__/user_mock_data_helper.js +++ b/spec/frontend/__helpers__/user_mock_data_helper.js @@ -15,7 +15,7 @@ export default { id: id + 1, name: getRandomString(), username: getRandomString(), - user_path: getRandomUrl(), + web_url: getRandomUrl(), }); id += 1; diff --git a/spec/frontend/__helpers__/vuex_action_helper.js b/spec/frontend/__helpers__/vuex_action_helper.js index 95a811d0385..ab2637d6024 100644 --- a/spec/frontend/__helpers__/vuex_action_helper.js +++ b/spec/frontend/__helpers__/vuex_action_helper.js @@ -1,5 +1,3 @@ -const noop = () => {}; - /** * Helper for testing action with expected mutations inspired in * https://vuex.vuejs.org/en/testing.html @@ -9,7 +7,6 @@ const noop = () => {}; * @param {Object} state will be provided to the action * @param {Array} [expectedMutations=[]] mutations expected to be committed * @param {Array} [expectedActions=[]] actions expected to be dispatched - * @param {Function} [done=noop] to be executed after the tests * @return {Promise} * * @example @@ -27,20 +24,9 @@ const noop = () => {}; * { type: 'actionName', payload: {param: 'foobar'}}, * { type: 'actionName1'} * ] - * done, * ); * * @example - * testAction( - * actions.actionName, // action - * { }, // mocked payload - * state, //state - * [ { type: types.MUTATION} ], // expected mutations - * [], // expected actions - * ).then(done) - * .catch(done.fail); - * - * @example * await testAction({ * action: actions.actionName, * payload: { deleteListId: 1 }, @@ -56,24 +42,15 @@ export default ( stateArg, expectedMutationsArg = [], expectedActionsArg = [], - doneArg = noop, ) => { let action = actionArg; let payload = payloadArg; let state = stateArg; let expectedMutations = expectedMutationsArg; let expectedActions = expectedActionsArg; - let done = doneArg; if (typeof actionArg !== 'function') { - ({ - action, - payload, - state, - expectedMutations = [], - expectedActions = [], - done = noop, - } = actionArg); + ({ action, payload, state, expectedMutations = [], expectedActions = [] } = actionArg); } const mutations = []; @@ -109,7 +86,6 @@ export default ( mutations: expectedMutations, actions: expectedActions, }); - done(); }; const result = action( @@ -117,8 +93,13 @@ export default ( payload, ); - // eslint-disable-next-line no-restricted-syntax - return (result || new Promise((resolve) => setImmediate(resolve))) + return ( + result || + new Promise((resolve) => { + // eslint-disable-next-line no-restricted-syntax + setImmediate(resolve); + }) + ) .catch((error) => { validateResults(); throw error; diff --git a/spec/frontend/__helpers__/vuex_action_helper_spec.js b/spec/frontend/__helpers__/vuex_action_helper_spec.js index b4f5a291774..5bb2b3b26e2 100644 --- a/spec/frontend/__helpers__/vuex_action_helper_spec.js +++ b/spec/frontend/__helpers__/vuex_action_helper_spec.js @@ -4,8 +4,8 @@ import axios from '~/lib/utils/axios_utils'; import testActionFn from './vuex_action_helper'; const testActionFnWithOptionsArg = (...args) => { - const [action, payload, state, expectedMutations, expectedActions, done] = args; - return testActionFn({ action, payload, state, expectedMutations, expectedActions, done }); + const [action, payload, state, expectedMutations, expectedActions] = args; + return testActionFn({ action, payload, state, expectedMutations, expectedActions }); }; describe.each([testActionFn, testActionFnWithOptionsArg])( @@ -14,7 +14,6 @@ describe.each([testActionFn, testActionFnWithOptionsArg])( let originalExpect; let assertion; let mock; - const noop = () => {}; beforeEach(() => { mock = new MockAdapter(axios); @@ -48,7 +47,7 @@ describe.each([testActionFn, testActionFnWithOptionsArg])( assertion = { mutations: [], actions: [] }; - testAction(action, examplePayload, exampleState); + return testAction(action, examplePayload, exampleState); }); describe('given a sync action', () => { @@ -59,7 +58,7 @@ describe.each([testActionFn, testActionFnWithOptionsArg])( assertion = { mutations: [{ type: 'MUTATION' }], actions: [] }; - testAction(action, null, {}, assertion.mutations, assertion.actions, noop); + return testAction(action, null, {}, assertion.mutations, assertion.actions); }); it('mocks dispatching actions', () => { @@ -69,26 +68,21 @@ describe.each([testActionFn, testActionFnWithOptionsArg])( assertion = { actions: [{ type: 'ACTION' }], mutations: [] }; - testAction(action, null, {}, assertion.mutations, assertion.actions, noop); + return testAction(action, null, {}, assertion.mutations, assertion.actions); }); - it('works with done callback once finished', (done) => { + it('returns a promise', () => { assertion = { mutations: [], actions: [] }; - testAction(noop, null, {}, assertion.mutations, assertion.actions, done); - }); + const promise = testAction(() => {}, null, {}, assertion.mutations, assertion.actions); - it('returns a promise', (done) => { - assertion = { mutations: [], actions: [] }; + originalExpect(promise instanceof Promise).toBeTruthy(); - testAction(noop, null, {}, assertion.mutations, assertion.actions) - .then(done) - .catch(done.fail); + return promise; }); }); describe('given an async action (returning a promise)', () => { - let lastError; const data = { FOO: 'BAR' }; const asyncAction = ({ commit, dispatch }) => { @@ -98,7 +92,6 @@ describe.each([testActionFn, testActionFnWithOptionsArg])( .get(TEST_HOST) .catch((error) => { commit('ERROR'); - lastError = error; throw error; }) .then(() => { @@ -107,46 +100,26 @@ describe.each([testActionFn, testActionFnWithOptionsArg])( }); }; - beforeEach(() => { - lastError = null; - }); - - it('works with done callback once finished', (done) => { + it('returns original data of successful promise while checking actions/mutations', async () => { mock.onGet(TEST_HOST).replyOnce(200, 42); assertion = { mutations: [{ type: 'SUCCESS' }], actions: [{ type: 'ACTION' }] }; - testAction(asyncAction, null, {}, assertion.mutations, assertion.actions, done); + const res = await testAction(asyncAction, null, {}, assertion.mutations, assertion.actions); + originalExpect(res).toEqual(data); }); - it('returns original data of successful promise while checking actions/mutations', (done) => { - mock.onGet(TEST_HOST).replyOnce(200, 42); - - assertion = { mutations: [{ type: 'SUCCESS' }], actions: [{ type: 'ACTION' }] }; - - testAction(asyncAction, null, {}, assertion.mutations, assertion.actions) - .then((res) => { - originalExpect(res).toEqual(data); - done(); - }) - .catch(done.fail); - }); - - it('returns original error of rejected promise while checking actions/mutations', (done) => { + it('returns original error of rejected promise while checking actions/mutations', async () => { mock.onGet(TEST_HOST).replyOnce(500, ''); assertion = { mutations: [{ type: 'ERROR' }], actions: [{ type: 'ACTION' }] }; - testAction(asyncAction, null, {}, assertion.mutations, assertion.actions) - .then(done.fail) - .catch((error) => { - originalExpect(error).toBe(lastError); - done(); - }); + const err = testAction(asyncAction, null, {}, assertion.mutations, assertion.actions); + await originalExpect(err).rejects.toEqual(new Error('Request failed with status code 500')); }); }); - it('works with async actions not returning promises', (done) => { + it('works with actions not returning promises', () => { const data = { FOO: 'BAR' }; const asyncAction = ({ commit, dispatch }) => { @@ -168,7 +141,7 @@ describe.each([testActionFn, testActionFnWithOptionsArg])( assertion = { mutations: [{ type: 'SUCCESS' }], actions: [{ type: 'ACTION' }] }; - testAction(asyncAction, null, {}, assertion.mutations, assertion.actions, done); + return testAction(asyncAction, null, {}, assertion.mutations, assertion.actions); }); }, ); diff --git a/spec/frontend/__helpers__/wait_for_promises.js b/spec/frontend/__helpers__/wait_for_promises.js index 2fd1cc6ba0d..753c3c5d92b 100644 --- a/spec/frontend/__helpers__/wait_for_promises.js +++ b/spec/frontend/__helpers__/wait_for_promises.js @@ -1 +1,4 @@ -export default () => new Promise((resolve) => requestAnimationFrame(resolve)); +export default () => + new Promise((resolve) => { + requestAnimationFrame(resolve); + }); |