From 71786ddc8e28fbd3cb3fcc4b3ff15e5962a1c82e Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Mon, 20 Feb 2023 13:49:51 +0000 Subject: Add latest changes from gitlab-org/gitlab@15-9-stable-ee --- spec/frontend/lib/apollo/persist_link_spec.js | 74 +++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 spec/frontend/lib/apollo/persist_link_spec.js (limited to 'spec/frontend/lib/apollo/persist_link_spec.js') diff --git a/spec/frontend/lib/apollo/persist_link_spec.js b/spec/frontend/lib/apollo/persist_link_spec.js new file mode 100644 index 00000000000..ddb861bcee0 --- /dev/null +++ b/spec/frontend/lib/apollo/persist_link_spec.js @@ -0,0 +1,74 @@ +/* eslint-disable no-underscore-dangle */ +import { gql, execute, ApolloLink, Observable } from '@apollo/client/core'; +import { testApolloLink } from 'helpers/test_apollo_link'; +import { getPersistLink } from '~/lib/apollo/persist_link'; + +const DEFAULT_QUERY = gql` + query { + foo { + bar + } + } +`; + +const QUERY_WITH_DIRECTIVE = gql` + query { + foo @persist { + bar + } + } +`; + +const QUERY_WITH_PERSIST_FIELD = gql` + query { + foo @persist { + bar + __persist + } + } +`; + +const terminatingLink = new ApolloLink(() => Observable.of({ data: { foo: { bar: 1 } } })); + +describe('~/lib/apollo/persist_link', () => { + let subscription; + + afterEach(() => { + if (subscription) { + subscription.unsubscribe(); + } + }); + + it('removes `@persist` directive from the operation', async () => { + const operation = await testApolloLink(getPersistLink(), {}, QUERY_WITH_DIRECTIVE); + const { selections } = operation.query.definitions[0].selectionSet; + + expect(selections[0].directives).toEqual([]); + }); + + it('removes `__persist` fields from the operation with `@persist` directive', async () => { + const operation = await testApolloLink(getPersistLink(), {}, QUERY_WITH_PERSIST_FIELD); + + const { selections } = operation.query.definitions[0].selectionSet; + const childFields = selections[0].selectionSet.selections; + + expect(childFields).toHaveLength(1); + expect(childFields.some((field) => field.name.value === '__persist')).toBe(false); + }); + + it('decorates the response with `__persist: true` is there is `__persist` field in the query', async () => { + const link = getPersistLink().concat(terminatingLink); + + subscription = execute(link, { query: QUERY_WITH_PERSIST_FIELD }).subscribe(({ data }) => { + expect(data.foo.__persist).toBe(true); + }); + }); + + it('does not decorate the response with `__persist: true` is there if query is not persistent', async () => { + const link = getPersistLink().concat(terminatingLink); + + subscription = execute(link, { query: DEFAULT_QUERY }).subscribe(({ data }) => { + expect(data.foo.__persist).toBe(undefined); + }); + }); +}); -- cgit v1.2.3