2022-08-27 11:52:29 +05:30
|
|
|
import { gql } from '@apollo/client/core';
|
2022-06-21 17:19:12 +05:30
|
|
|
import createApolloClient from '~/lib/graphql';
|
|
|
|
import { createLocalState } from '~/runner/graphql/list/local_state';
|
|
|
|
import getCheckedRunnerIdsQuery from '~/runner/graphql/list/checked_runner_ids.query.graphql';
|
2022-08-27 11:52:29 +05:30
|
|
|
import { RUNNER_TYPENAME } from '~/runner/constants';
|
2022-06-21 17:19:12 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
const makeRunner = (id, deleteRunner = true) => ({
|
|
|
|
id,
|
|
|
|
userPermissions: {
|
|
|
|
deleteRunner,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
describe('~/runner/graphql/list/local_state', () => {
|
|
|
|
let localState;
|
|
|
|
let apolloClient;
|
|
|
|
|
|
|
|
const createSubject = () => {
|
|
|
|
if (apolloClient) {
|
|
|
|
throw new Error('test subject already exists!');
|
|
|
|
}
|
|
|
|
|
|
|
|
localState = createLocalState();
|
|
|
|
|
|
|
|
const { cacheConfig, typeDefs } = localState;
|
|
|
|
|
|
|
|
apolloClient = createApolloClient({}, { cacheConfig, typeDefs });
|
|
|
|
};
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
const addMockRunnerToCache = (id) => {
|
|
|
|
// mock some runners in the cache to prevent dangling references
|
|
|
|
apolloClient.writeFragment({
|
|
|
|
id: `${RUNNER_TYPENAME}:${id}`,
|
|
|
|
fragment: gql`
|
|
|
|
fragment DummyRunner on CiRunner {
|
|
|
|
__typename
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
data: {
|
|
|
|
__typename: RUNNER_TYPENAME,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
const queryCheckedRunnerIds = () => {
|
|
|
|
const { checkedRunnerIds } = apolloClient.readQuery({
|
|
|
|
query: getCheckedRunnerIdsQuery,
|
|
|
|
});
|
|
|
|
return checkedRunnerIds;
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
createSubject();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
localState = null;
|
|
|
|
apolloClient = null;
|
|
|
|
});
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
describe('queryCheckedRunnerIds', () => {
|
|
|
|
it('has empty checked list by default', () => {
|
2022-06-21 17:19:12 +05:30
|
|
|
expect(queryCheckedRunnerIds()).toEqual([]);
|
|
|
|
});
|
2022-08-27 11:52:29 +05:30
|
|
|
|
|
|
|
it('returns checked runners that have a reference in the cache', () => {
|
2022-11-25 23:54:43 +05:30
|
|
|
const id = 'a';
|
|
|
|
|
|
|
|
addMockRunnerToCache(id);
|
|
|
|
localState.localMutations.setRunnerChecked({
|
|
|
|
runner: makeRunner(id),
|
|
|
|
isChecked: true,
|
|
|
|
});
|
2022-08-27 11:52:29 +05:30
|
|
|
|
|
|
|
expect(queryCheckedRunnerIds()).toEqual(['a']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('return checked runners that are not dangling references', () => {
|
|
|
|
addMockRunnerToCache('a'); // 'b' is missing from the cache, perhaps because it was deleted
|
2022-11-25 23:54:43 +05:30
|
|
|
localState.localMutations.setRunnerChecked({ runner: makeRunner('a'), isChecked: true });
|
|
|
|
localState.localMutations.setRunnerChecked({ runner: makeRunner('b'), isChecked: true });
|
2022-08-27 11:52:29 +05:30
|
|
|
|
|
|
|
expect(queryCheckedRunnerIds()).toEqual(['a']);
|
|
|
|
});
|
2022-06-21 17:19:12 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
describe.each`
|
|
|
|
inputs | expected
|
|
|
|
${[['a', true], ['b', true], ['b', true]]} | ${['a', 'b']}
|
|
|
|
${[['a', true], ['b', true], ['a', false]]} | ${['b']}
|
|
|
|
${[['c', true], ['b', true], ['a', true], ['d', false]]} | ${['c', 'b', 'a']}
|
|
|
|
`('setRunnerChecked', ({ inputs, expected }) => {
|
|
|
|
beforeEach(() => {
|
|
|
|
inputs.forEach(([id, isChecked]) => {
|
2022-08-27 11:52:29 +05:30
|
|
|
addMockRunnerToCache(id);
|
2022-11-25 23:54:43 +05:30
|
|
|
localState.localMutations.setRunnerChecked({ runner: makeRunner(id), isChecked });
|
2022-06-21 17:19:12 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
it(`for inputs="${inputs}" has a ids="[${expected}]"`, () => {
|
|
|
|
expect(queryCheckedRunnerIds()).toEqual(expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
describe.each`
|
|
|
|
inputs | expected
|
|
|
|
${[[['a', 'b'], true]]} | ${['a', 'b']}
|
|
|
|
${[[['a', 'b'], false]]} | ${[]}
|
|
|
|
${[[['a', 'b'], true], [['c', 'd'], true]]} | ${['a', 'b', 'c', 'd']}
|
|
|
|
${[[['a', 'b'], true], [['a', 'b'], false]]} | ${[]}
|
|
|
|
${[[['a', 'b'], true], [['b'], false]]} | ${['a']}
|
|
|
|
`('setRunnersChecked', ({ inputs, expected }) => {
|
|
|
|
beforeEach(() => {
|
|
|
|
inputs.forEach(([ids, isChecked]) => {
|
|
|
|
ids.forEach(addMockRunnerToCache);
|
|
|
|
|
|
|
|
localState.localMutations.setRunnersChecked({
|
2022-11-25 23:54:43 +05:30
|
|
|
runners: ids.map((id) => makeRunner(id)),
|
2022-08-27 11:52:29 +05:30
|
|
|
isChecked,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`for inputs="${inputs}" has a ids="[${expected}]"`, () => {
|
|
|
|
expect(queryCheckedRunnerIds()).toEqual(expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
describe('clearChecked', () => {
|
|
|
|
it('clears all checked items', () => {
|
|
|
|
['a', 'b', 'c'].forEach((id) => {
|
2022-08-27 11:52:29 +05:30
|
|
|
addMockRunnerToCache(id);
|
2022-11-25 23:54:43 +05:30
|
|
|
localState.localMutations.setRunnerChecked({ runner: makeRunner(id), isChecked: true });
|
2022-06-21 17:19:12 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
expect(queryCheckedRunnerIds()).toEqual(['a', 'b', 'c']);
|
|
|
|
|
|
|
|
localState.localMutations.clearChecked();
|
|
|
|
|
|
|
|
expect(queryCheckedRunnerIds()).toEqual([]);
|
|
|
|
});
|
|
|
|
});
|
2022-11-25 23:54:43 +05:30
|
|
|
|
|
|
|
describe('when some runners cannot be deleted', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
addMockRunnerToCache('a');
|
|
|
|
addMockRunnerToCache('b');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('setRunnerChecked does not check runner that cannot be deleted', () => {
|
|
|
|
localState.localMutations.setRunnerChecked({
|
|
|
|
runner: makeRunner('a', false),
|
|
|
|
isChecked: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(queryCheckedRunnerIds()).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('setRunnersChecked does not check runner that cannot be deleted', () => {
|
|
|
|
localState.localMutations.setRunnersChecked({
|
|
|
|
runners: [makeRunner('a', false), makeRunner('b', false)],
|
|
|
|
isChecked: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(queryCheckedRunnerIds()).toEqual([]);
|
|
|
|
});
|
|
|
|
});
|
2022-06-21 17:19:12 +05:30
|
|
|
});
|