2020-11-27 10:09:34 -05:00
|
|
|
import {
|
2021-08-02 08:09:59 -04:00
|
|
|
isGid,
|
2020-11-27 10:09:34 -05:00
|
|
|
getIdFromGraphQLId,
|
|
|
|
convertToGraphQLId,
|
|
|
|
convertToGraphQLIds,
|
2021-03-10 10:09:11 -05:00
|
|
|
convertFromGraphQLIds,
|
|
|
|
convertNodeIdsFromGraphQLIds,
|
2020-11-27 10:09:34 -05:00
|
|
|
} from '~/graphql_shared/utils';
|
|
|
|
|
|
|
|
const mockType = 'Group';
|
|
|
|
const mockId = 12;
|
|
|
|
const mockGid = `gid://gitlab/Group/12`;
|
2020-01-24 10:09:00 -05:00
|
|
|
|
2021-08-02 08:09:59 -04:00
|
|
|
describe('isGid', () => {
|
|
|
|
it('returns true if passed id is gid', () => {
|
|
|
|
expect(isGid(mockGid)).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns false if passed id is not gid', () => {
|
|
|
|
expect(isGid(mockId)).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-01-24 10:09:00 -05:00
|
|
|
describe('getIdFromGraphQLId', () => {
|
|
|
|
[
|
|
|
|
{
|
|
|
|
input: '',
|
|
|
|
output: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: null,
|
|
|
|
output: null,
|
|
|
|
},
|
2020-11-09 07:09:24 -05:00
|
|
|
{
|
|
|
|
input: 2,
|
|
|
|
output: 2,
|
|
|
|
},
|
2020-01-24 10:09:00 -05:00
|
|
|
{
|
|
|
|
input: 'gid://',
|
|
|
|
output: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: 'gid://gitlab/',
|
|
|
|
output: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: 'gid://gitlab/Environments',
|
|
|
|
output: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: 'gid://gitlab/Environments/',
|
|
|
|
output: null,
|
|
|
|
},
|
2021-11-08 22:42:22 -05:00
|
|
|
{
|
|
|
|
input: 'gid://gitlab/Environments/0',
|
2021-11-09 22:10:14 -05:00
|
|
|
output: 0,
|
2021-11-08 22:42:22 -05:00
|
|
|
},
|
2020-01-24 10:09:00 -05:00
|
|
|
{
|
|
|
|
input: 'gid://gitlab/Environments/123',
|
|
|
|
output: 123,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: 'gid://gitlab/DesignManagement::Version/2',
|
|
|
|
output: 2,
|
|
|
|
},
|
|
|
|
].forEach(({ input, output }) => {
|
|
|
|
it(`getIdFromGraphQLId returns ${output} when passed ${input}`, () => {
|
|
|
|
expect(getIdFromGraphQLId(input)).toBe(output);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-11-27 10:09:34 -05:00
|
|
|
|
|
|
|
describe('convertToGraphQLId', () => {
|
|
|
|
it('combines $type and $id into $result', () => {
|
|
|
|
expect(convertToGraphQLId(mockType, mockId)).toBe(mockGid);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
type | id | message
|
|
|
|
${mockType} | ${null} | ${'id must be a number or string; got object'}
|
|
|
|
${null} | ${mockId} | ${'type must be a string; got object'}
|
|
|
|
`('throws TypeError with "$message" if a param is missing', ({ type, id, message }) => {
|
|
|
|
expect(() => convertToGraphQLId(type, id)).toThrow(new TypeError(message));
|
|
|
|
});
|
2021-08-02 08:09:59 -04:00
|
|
|
|
|
|
|
it('returns id as is if it follows the gid format', () => {
|
|
|
|
expect(convertToGraphQLId(mockType, mockGid)).toStrictEqual(mockGid);
|
|
|
|
});
|
2020-11-27 10:09:34 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('convertToGraphQLIds', () => {
|
|
|
|
it('combines $type and $id into $result', () => {
|
|
|
|
expect(convertToGraphQLIds(mockType, [mockId])).toStrictEqual([mockGid]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
type | ids | message
|
2022-03-14 17:08:17 -04:00
|
|
|
${mockType} | ${null} | ${"Cannot read properties of null (reading 'map')"}
|
2020-11-27 10:09:34 -05:00
|
|
|
${mockType} | ${[mockId, null]} | ${'id must be a number or string; got object'}
|
|
|
|
${null} | ${[mockId]} | ${'type must be a string; got object'}
|
|
|
|
`('throws TypeError with "$message" if a param is missing', ({ type, ids, message }) => {
|
|
|
|
expect(() => convertToGraphQLIds(type, ids)).toThrow(new TypeError(message));
|
|
|
|
});
|
|
|
|
});
|
2021-03-10 10:09:11 -05:00
|
|
|
|
|
|
|
describe('convertFromGraphQLIds', () => {
|
|
|
|
it.each`
|
|
|
|
ids | expected
|
|
|
|
${[mockGid]} | ${[mockId]}
|
|
|
|
${[mockGid, 'invalid id']} | ${[mockId, null]}
|
|
|
|
`('converts $ids from GraphQL Ids', ({ ids, expected }) => {
|
|
|
|
expect(convertFromGraphQLIds(ids)).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("throws TypeError if `ids` parameter isn't an array", () => {
|
|
|
|
expect(() => convertFromGraphQLIds('invalid')).toThrow(
|
|
|
|
new TypeError('ids must be an array; got string'),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('convertNodeIdsFromGraphQLIds', () => {
|
|
|
|
it.each`
|
|
|
|
nodes | expected
|
|
|
|
${[{ id: mockGid, name: 'foo bar' }, { id: mockGid, name: 'baz' }]} | ${[{ id: mockId, name: 'foo bar' }, { id: mockId, name: 'baz' }]}
|
|
|
|
${[{ name: 'foo bar' }]} | ${[{ name: 'foo bar' }]}
|
|
|
|
`('converts `id` properties in $nodes from GraphQL Id', ({ nodes, expected }) => {
|
|
|
|
expect(convertNodeIdsFromGraphQLIds(nodes)).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("throws TypeError if `nodes` parameter isn't an array", () => {
|
|
|
|
expect(() => convertNodeIdsFromGraphQLIds('invalid')).toThrow(
|
|
|
|
new TypeError('nodes must be an array; got string'),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|