Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'spec/frontend/lib/apollo/persist_link_spec.js')
-rw-r--r--spec/frontend/lib/apollo/persist_link_spec.js74
1 files changed, 74 insertions, 0 deletions
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);
+ });
+ });
+});