From 47b8c698529326f9a8baaa54f806d970add50547 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Fri, 15 Dec 2023 13:19:45 +0530 Subject: [PATCH] chore: improve tests --- tests/fixtures.rs | 65 +++- .../definations/fragment/input.js | 0 .../definations/fragment/output.js | 0 .../definations/fragment/strip-output.js | 51 +++ .../definations/operation/mutation/input.js | 0 .../definations/operation/mutation/output.js | 0 .../operation/mutation/strip-output.js | 71 ++++ .../definations/operation/query/input.js | 0 .../definations/operation/query/output.js | 0 .../operation/query/strip-output.js | 60 +++ .../directives/input.js | 0 .../directives/output.js | 0 tests/graphql_tag/directives/strip-output.js | 245 ++++++++++++ .../dynamicSegments/input.js | 0 .../dynamicSegments/output.js | 0 .../dynamicSegments/strip-output.js | 99 +++++ .../importSources/input.js | 0 .../importSources/output.js | 0 .../graphql_tag/importSources/strip-output.js | 83 ++++ .../selections/input.js | 0 .../selections/output.js | 0 tests/graphql_tag/selections/strip-output.js | 353 ++++++++++++++++++ tests/{fixtures => graphql_tag}/type/input.js | 0 .../{fixtures => graphql_tag}/type/output.js | 0 tests/graphql_tag/type/strip-output.js | 237 ++++++++++++ .../usage/defaultExport/input.js | 0 .../usage/defaultExport/output.js | 0 .../usage/defaultExport/strip-output.js | 114 ++++++ .../usage/returnFromArrowFunction/input.js | 0 .../usage/returnFromArrowFunction/output.js | 0 .../returnFromArrowFunction/strip-output.js | 114 ++++++ .../returnFromFunctionExpression/input.js | 0 .../returnFromFunctionExpression/output.js | 0 .../strip-output.js | 116 ++++++ .../{fixtures => graphql_tag}/values/input.js | 0 .../values/output.js | 0 tests/graphql_tag/values/strip-output.js | 264 +++++++++++++ tests/unique_identifier/no_unique/input.js | 14 + tests/unique_identifier/no_unique/output.txt | 2 + .../with_unique_import/input.js | 15 + .../with_unique_import/output.txt | 2 + .../with_unique_local/input.js | 15 + .../with_unique_local/output.txt | 2 + 43 files changed, 1918 insertions(+), 4 deletions(-) rename tests/{fixtures => graphql_tag}/definations/fragment/input.js (100%) rename tests/{fixtures => graphql_tag}/definations/fragment/output.js (100%) create mode 100644 tests/graphql_tag/definations/fragment/strip-output.js rename tests/{fixtures => graphql_tag}/definations/operation/mutation/input.js (100%) rename tests/{fixtures => graphql_tag}/definations/operation/mutation/output.js (100%) create mode 100644 tests/graphql_tag/definations/operation/mutation/strip-output.js rename tests/{fixtures => graphql_tag}/definations/operation/query/input.js (100%) rename tests/{fixtures => graphql_tag}/definations/operation/query/output.js (100%) create mode 100644 tests/graphql_tag/definations/operation/query/strip-output.js rename tests/{fixtures => graphql_tag}/directives/input.js (100%) rename tests/{fixtures => graphql_tag}/directives/output.js (100%) create mode 100644 tests/graphql_tag/directives/strip-output.js rename tests/{fixtures => graphql_tag}/dynamicSegments/input.js (100%) rename tests/{fixtures => graphql_tag}/dynamicSegments/output.js (100%) create mode 100644 tests/graphql_tag/dynamicSegments/strip-output.js rename tests/{fixtures => graphql_tag}/importSources/input.js (100%) rename tests/{fixtures => graphql_tag}/importSources/output.js (100%) create mode 100644 tests/graphql_tag/importSources/strip-output.js rename tests/{fixtures => graphql_tag}/selections/input.js (100%) rename tests/{fixtures => graphql_tag}/selections/output.js (100%) create mode 100644 tests/graphql_tag/selections/strip-output.js rename tests/{fixtures => graphql_tag}/type/input.js (100%) rename tests/{fixtures => graphql_tag}/type/output.js (100%) create mode 100644 tests/graphql_tag/type/strip-output.js rename tests/{fixtures => graphql_tag}/usage/defaultExport/input.js (100%) rename tests/{fixtures => graphql_tag}/usage/defaultExport/output.js (100%) create mode 100644 tests/graphql_tag/usage/defaultExport/strip-output.js rename tests/{fixtures => graphql_tag}/usage/returnFromArrowFunction/input.js (100%) rename tests/{fixtures => graphql_tag}/usage/returnFromArrowFunction/output.js (100%) create mode 100644 tests/graphql_tag/usage/returnFromArrowFunction/strip-output.js rename tests/{fixtures => graphql_tag}/usage/returnFromFunctionExpression/input.js (100%) rename tests/{fixtures => graphql_tag}/usage/returnFromFunctionExpression/output.js (100%) create mode 100644 tests/graphql_tag/usage/returnFromFunctionExpression/strip-output.js rename tests/{fixtures => graphql_tag}/values/input.js (100%) rename tests/{fixtures => graphql_tag}/values/output.js (100%) create mode 100644 tests/graphql_tag/values/strip-output.js create mode 100644 tests/unique_identifier/no_unique/input.js create mode 100644 tests/unique_identifier/no_unique/output.txt create mode 100644 tests/unique_identifier/with_unique_import/input.js create mode 100644 tests/unique_identifier/with_unique_import/output.txt create mode 100644 tests/unique_identifier/with_unique_local/input.js create mode 100644 tests/unique_identifier/with_unique_local/output.txt diff --git a/tests/fixtures.rs b/tests/fixtures.rs index 9ef7233..88f20ec 100644 --- a/tests/fixtures.rs +++ b/tests/fixtures.rs @@ -1,14 +1,15 @@ // built-ins -use std::path::PathBuf; +use std::{fs, path::PathBuf}; // libs -use swc_core::ecma::transforms::testing::{test_fixture, FixtureTestConfig}; +use swc_core::ecma::transforms::testing::{test_fixture, FixtureTestConfig, Tester}; use swc_ecma_parser::{EsConfig, Syntax}; use swc_ecma_visit::as_folder; -use testing::fixture; +use testing::{fixture, NormalizedOutput}; // structs use graphql_tag::structs::{GraphQLTagConfig, TransformVisitor}; +use unique_identifier::UniqueIdentifierVisitor; fn syntax() -> Syntax { Syntax::Es(EsConfig { @@ -17,11 +18,13 @@ fn syntax() -> Syntax { }) } -#[fixture("tests/fixtures/**/input.js")] +#[fixture("tests/graphql_tag/**/input.js")] fn graphql_tag_fixture(input: PathBuf) { let dir = input.parent().unwrap(); let output = dir.join("output.js"); + let strip_output = dir.join("strip-output.js"); + // With strip false test_fixture( syntax(), &|_tr| { @@ -43,4 +46,58 @@ fn graphql_tag_fixture(input: PathBuf) { sourcemap: false, }, ); + + // With strip true + test_fixture( + syntax(), + &|_tr| { + as_folder(TransformVisitor::new( + GraphQLTagConfig { + import_sources: vec!["@apollo/client".to_string(), "graphql-tag".into()], + gql_tag_identifiers: vec!["gql".to_string()], + strip: true, + unique_fn_name: "unique".into(), + unique_fn_used: false, + }, + _tr.comments.clone(), + )) + }, + &input, + &strip_output, + FixtureTestConfig { + allow_error: true, + sourcemap: false, + }, + ); +} + +#[fixture("tests/unique_identifier/**/input.js")] +fn unique_identifier_fixture(input: PathBuf) { + let dir = input.parent().unwrap(); + let output = dir.join("output.txt"); + + let expected: Result = fs::read_to_string(&output); + let expected = expected.unwrap_or_default(); + + let input_text = fs::read_to_string(input).unwrap(); + let actual_output = Tester::run(|tester| { + let mut unique_visitor = UniqueIdentifierVisitor::new(); + tester.apply_transform( + as_folder(&mut unique_visitor), + "noop.js", + syntax(), + &input_text, + )?; + + Ok(format!( + "identifier: {}\ncount: {}", + unique_visitor.identifier, unique_visitor.count + )) + }); + + if actual_output != expected { + NormalizedOutput::from(actual_output) + .compare_to_file(output) + .unwrap(); + } } diff --git a/tests/fixtures/definations/fragment/input.js b/tests/graphql_tag/definations/fragment/input.js similarity index 100% rename from tests/fixtures/definations/fragment/input.js rename to tests/graphql_tag/definations/fragment/input.js diff --git a/tests/fixtures/definations/fragment/output.js b/tests/graphql_tag/definations/fragment/output.js similarity index 100% rename from tests/fixtures/definations/fragment/output.js rename to tests/graphql_tag/definations/fragment/output.js diff --git a/tests/graphql_tag/definations/fragment/strip-output.js b/tests/graphql_tag/definations/fragment/strip-output.js new file mode 100644 index 0000000..4c4a661 --- /dev/null +++ b/tests/graphql_tag/definations/fragment/strip-output.js @@ -0,0 +1,51 @@ +import { gql } from "@apollo/client"; +const FRAGMENT = { + "kind": "Document", + "definitions": [ + { + "kind": "FragmentDefinition", + "name": { + "kind": "Name", + "value": "TestFragment" + }, + "directives": [], + "typeCondition": { + "kind": "NamedType", + "name": { + "kind": "Name", + "value": "Entity" + } + }, + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "id" + }, + "arguments": [], + "directives": [] + }, + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "name" + }, + "arguments": [], + "directives": [] + } + ] + } + } + ], + "loc": { + "start": 0, + "end": 40, + "source": { + "body": "fragment TestFragment on Entity{id name}" + } + } +}; diff --git a/tests/fixtures/definations/operation/mutation/input.js b/tests/graphql_tag/definations/operation/mutation/input.js similarity index 100% rename from tests/fixtures/definations/operation/mutation/input.js rename to tests/graphql_tag/definations/operation/mutation/input.js diff --git a/tests/fixtures/definations/operation/mutation/output.js b/tests/graphql_tag/definations/operation/mutation/output.js similarity index 100% rename from tests/fixtures/definations/operation/mutation/output.js rename to tests/graphql_tag/definations/operation/mutation/output.js diff --git a/tests/graphql_tag/definations/operation/mutation/strip-output.js b/tests/graphql_tag/definations/operation/mutation/strip-output.js new file mode 100644 index 0000000..4fcd1ee --- /dev/null +++ b/tests/graphql_tag/definations/operation/mutation/strip-output.js @@ -0,0 +1,71 @@ +import { gql } from "@apollo/client"; +const MUTATION = { + "kind": "Document", + "definitions": [ + { + "kind": "OperationDefinition", + "name": { + "kind": "Name", + "value": "testMutation" + }, + "directives": [], + "variableDefinitions": [ + { + "kind": "VariableDefinition", + "directives": [], + "variable": { + "kind": "Variable", + "name": { + "kind": "Name", + "value": "entity" + } + }, + "type": { + "kind": "NamedType", + "name": { + "kind": "Name", + "value": "String" + } + } + } + ], + "operation": "mutation", + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "updateEntity" + }, + "arguments": [ + { + "kind": "Argument", + "name": { + "kind": "Name", + "value": "entity" + }, + "value": { + "kind": "Variable", + "name": { + "kind": "Name", + "value": "entity" + } + } + } + ], + "directives": [] + } + ] + } + } + ], + "loc": { + "start": 0, + "end": 67, + "source": { + "body": "mutation testMutation($entity:String){updateEntity(entity:$entity)}" + } + } +}; diff --git a/tests/fixtures/definations/operation/query/input.js b/tests/graphql_tag/definations/operation/query/input.js similarity index 100% rename from tests/fixtures/definations/operation/query/input.js rename to tests/graphql_tag/definations/operation/query/input.js diff --git a/tests/fixtures/definations/operation/query/output.js b/tests/graphql_tag/definations/operation/query/output.js similarity index 100% rename from tests/fixtures/definations/operation/query/output.js rename to tests/graphql_tag/definations/operation/query/output.js diff --git a/tests/graphql_tag/definations/operation/query/strip-output.js b/tests/graphql_tag/definations/operation/query/strip-output.js new file mode 100644 index 0000000..6678423 --- /dev/null +++ b/tests/graphql_tag/definations/operation/query/strip-output.js @@ -0,0 +1,60 @@ +import { gql } from "@apollo/client"; +const QUERY = { + "kind": "Document", + "definitions": [ + { + "kind": "OperationDefinition", + "name": { + "kind": "Name", + "value": "testQuery" + }, + "directives": [], + "variableDefinitions": [], + "operation": "query", + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "getEntity" + }, + "arguments": [], + "directives": [], + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "id" + }, + "arguments": [], + "directives": [] + }, + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "name" + }, + "arguments": [], + "directives": [] + } + ] + } + } + ] + } + } + ], + "loc": { + "start": 0, + "end": 35, + "source": { + "body": "query testQuery{getEntity{id name}}" + } + } +}; diff --git a/tests/fixtures/directives/input.js b/tests/graphql_tag/directives/input.js similarity index 100% rename from tests/fixtures/directives/input.js rename to tests/graphql_tag/directives/input.js diff --git a/tests/fixtures/directives/output.js b/tests/graphql_tag/directives/output.js similarity index 100% rename from tests/fixtures/directives/output.js rename to tests/graphql_tag/directives/output.js diff --git a/tests/graphql_tag/directives/strip-output.js b/tests/graphql_tag/directives/strip-output.js new file mode 100644 index 0000000..e86f589 --- /dev/null +++ b/tests/graphql_tag/directives/strip-output.js @@ -0,0 +1,245 @@ +import { gql } from "@apollo/client"; +// Simple directive +const SIMPLE_DIRECTIVE = { + "kind": "Document", + "definitions": [ + { + "kind": "OperationDefinition", + "name": { + "kind": "Name", + "value": "testQuery" + }, + "directives": [], + "variableDefinitions": [], + "operation": "query", + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "getEntity" + }, + "arguments": [], + "directives": [], + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "id" + }, + "arguments": [], + "directives": [ + { + "kind": "Directive", + "name": { + "kind": "Name", + "value": "directive" + } + } + ] + } + ] + } + } + ] + } + } + ], + "loc": { + "start": 0, + "end": 40, + "source": { + "body": "query testQuery{getEntity{id@directive}}" + } + } +}; +// directives with arguments +const ARGUMENTS_DIRECTIVE = { + "kind": "Document", + "definitions": [ + { + "kind": "OperationDefinition", + "name": { + "kind": "Name", + "value": "testQuery" + }, + "directives": [], + "variableDefinitions": [], + "operation": "query", + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "getEntity" + }, + "arguments": [], + "directives": [], + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "id" + }, + "arguments": [], + "directives": [] + }, + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "name" + }, + "arguments": [], + "directives": [ + { + "kind": "Directive", + "name": { + "kind": "Name", + "value": "directive" + }, + "arguments": [ + { + "kind": "Argument", + "name": { + "kind": "Name", + "value": "arg" + }, + "value": { + "kind": "StringValue", + "value": "argVal" + } + } + ] + } + ] + } + ] + } + } + ] + } + } + ], + "loc": { + "start": 0, + "end": 59, + "source": { + "body": 'query testQuery{getEntity{id name@directive(arg:"argVal")}}' + } + } +}; +// directives with selection set +const DIRECTIVES_SELECTION_SET = { + "kind": "Document", + "definitions": [ + { + "kind": "OperationDefinition", + "name": { + "kind": "Name", + "value": "testQuery" + }, + "directives": [], + "variableDefinitions": [], + "operation": "query", + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "getEntity" + }, + "arguments": [], + "directives": [], + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "id" + }, + "arguments": [], + "directives": [] + }, + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "user" + }, + "arguments": [], + "directives": [ + { + "kind": "Directive", + "name": { + "kind": "Name", + "value": "directive" + }, + "arguments": [ + { + "kind": "Argument", + "name": { + "kind": "Name", + "value": "arg" + }, + "value": { + "kind": "StringValue", + "value": "argVal" + } + } + ] + } + ], + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "id" + }, + "arguments": [], + "directives": [] + }, + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "name" + }, + "arguments": [], + "directives": [] + } + ] + } + } + ] + } + } + ] + } + } + ], + "loc": { + "start": 0, + "end": 68, + "source": { + "body": 'query testQuery{getEntity{id user@directive(arg:"argVal"){id name}}}' + } + } +}; diff --git a/tests/fixtures/dynamicSegments/input.js b/tests/graphql_tag/dynamicSegments/input.js similarity index 100% rename from tests/fixtures/dynamicSegments/input.js rename to tests/graphql_tag/dynamicSegments/input.js diff --git a/tests/fixtures/dynamicSegments/output.js b/tests/graphql_tag/dynamicSegments/output.js similarity index 100% rename from tests/fixtures/dynamicSegments/output.js rename to tests/graphql_tag/dynamicSegments/output.js diff --git a/tests/graphql_tag/dynamicSegments/strip-output.js b/tests/graphql_tag/dynamicSegments/strip-output.js new file mode 100644 index 0000000..4dfb020 --- /dev/null +++ b/tests/graphql_tag/dynamicSegments/strip-output.js @@ -0,0 +1,99 @@ +import { gql } from "@apollo/client"; +const unique = (definitions)=>{ + const names = {}; + return definitions.filter((definition)=>{ + if (definition.kind !== 'FragmentDefinition') { + return true; + } + const name = definition.name.value; + if (names[name]) { + return false; + } else { + names[name] = true; + return true; + } + }); +}; +const NAME = "LOL"; +const DYNAMIC_FRAGMENT = gql` + fragment name on ${NAME} { + id + } +`; +const QUERY_WITH_DYNAMIC_SEGMENT = gql` + query testQuery { + getEntity { + ... on ${NAME}{ + lol + } + } + } + + ${DYNAMIC_FRAGMENT} +`; +const STATIC_QUERY = { + "kind": "Document", + "definitions": /*#__PURE__*/ unique(/*#__PURE__*/ [ + { + "kind": "OperationDefinition", + "name": { + "kind": "Name", + "value": "testQuery" + }, + "directives": [], + "variableDefinitions": [], + "operation": "query", + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "getEntity" + }, + "arguments": [], + "directives": [], + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "InlineFragment", + "directives": [], + "typeCondition": { + "kind": "NamedType", + "name": { + "kind": "Name", + "value": "LOL" + } + }, + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "lol" + }, + "arguments": [], + "directives": [] + } + ] + } + } + ] + } + } + ] + } + } + ].concat(DYNAMIC_FRAGMENT.definitions)), + "loc": { + "start": 0, + "end": 42, + "source": { + "body": "query testQuery{getEntity{...on LOL{lol}}}" + } + } +}; diff --git a/tests/fixtures/importSources/input.js b/tests/graphql_tag/importSources/input.js similarity index 100% rename from tests/fixtures/importSources/input.js rename to tests/graphql_tag/importSources/input.js diff --git a/tests/fixtures/importSources/output.js b/tests/graphql_tag/importSources/output.js similarity index 100% rename from tests/fixtures/importSources/output.js rename to tests/graphql_tag/importSources/output.js diff --git a/tests/graphql_tag/importSources/strip-output.js b/tests/graphql_tag/importSources/strip-output.js new file mode 100644 index 0000000..56a79cf --- /dev/null +++ b/tests/graphql_tag/importSources/strip-output.js @@ -0,0 +1,83 @@ +import { gql as lol } from "@apollo/client"; +import baseGql from "graphql-tag"; +import { gql } from "@lol/client"; +// should not compile +const NEGATIVE_CASE = gql` + query testQuery { + getEntity + } +`; +// should compile +const POSITIVE_CASE_1 = { + "kind": "Document", + "definitions": [ + { + "kind": "OperationDefinition", + "name": { + "kind": "Name", + "value": "testQuery" + }, + "directives": [], + "variableDefinitions": [], + "operation": "query", + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "getEntity" + }, + "arguments": [], + "directives": [] + } + ] + } + } + ], + "loc": { + "start": 0, + "end": 26, + "source": { + "body": "query testQuery{getEntity}" + } + } +}; +// should compile +const POSITIVE_CASE_2 = { + "kind": "Document", + "definitions": [ + { + "kind": "OperationDefinition", + "name": { + "kind": "Name", + "value": "testQuery" + }, + "directives": [], + "variableDefinitions": [], + "operation": "query", + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "getEntity" + }, + "arguments": [], + "directives": [] + } + ] + } + } + ], + "loc": { + "start": 0, + "end": 26, + "source": { + "body": "query testQuery{getEntity}" + } + } +}; diff --git a/tests/fixtures/selections/input.js b/tests/graphql_tag/selections/input.js similarity index 100% rename from tests/fixtures/selections/input.js rename to tests/graphql_tag/selections/input.js diff --git a/tests/fixtures/selections/output.js b/tests/graphql_tag/selections/output.js similarity index 100% rename from tests/fixtures/selections/output.js rename to tests/graphql_tag/selections/output.js diff --git a/tests/graphql_tag/selections/strip-output.js b/tests/graphql_tag/selections/strip-output.js new file mode 100644 index 0000000..bb1da8d --- /dev/null +++ b/tests/graphql_tag/selections/strip-output.js @@ -0,0 +1,353 @@ +import { gql } from "@apollo/client"; +const NORMAL_SELECTION = { + "kind": "Document", + "definitions": [ + { + "kind": "OperationDefinition", + "name": { + "kind": "Name", + "value": "testQuery" + }, + "directives": [], + "variableDefinitions": [], + "operation": "query", + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "getEntity" + }, + "arguments": [], + "directives": [], + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "id" + }, + "arguments": [], + "directives": [] + }, + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "name" + }, + "arguments": [], + "directives": [] + } + ] + } + } + ] + } + } + ], + "loc": { + "start": 0, + "end": 35, + "source": { + "body": "query testQuery{getEntity{id name}}" + } + } +}; +const INLINE_FRAGMENT_SELECTION = { + "kind": "Document", + "definitions": [ + { + "kind": "OperationDefinition", + "name": { + "kind": "Name", + "value": "testQuery" + }, + "directives": [], + "variableDefinitions": [], + "operation": "query", + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "getEntity" + }, + "arguments": [], + "directives": [], + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "id" + }, + "arguments": [], + "directives": [] + }, + { + "kind": "InlineFragment", + "directives": [], + "typeCondition": { + "kind": "NamedType", + "name": { + "kind": "Name", + "value": "User" + } + }, + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "name" + }, + "arguments": [], + "directives": [] + }, + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "address" + }, + "arguments": [], + "directives": [] + } + ] + } + }, + { + "kind": "InlineFragment", + "directives": [], + "typeCondition": { + "kind": "NamedType", + "name": { + "kind": "Name", + "value": "Baby" + } + }, + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "name" + }, + "arguments": [], + "directives": [] + }, + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "parentAddress" + }, + "arguments": [], + "directives": [] + } + ] + } + } + ] + } + } + ] + } + } + ], + "loc": { + "start": 0, + "end": 84, + "source": { + "body": "query testQuery{getEntity{id...on User{name address}...on Baby{name parentAddress}}}" + } + } +}; +const USER_FRAGMENT = { + "kind": "Document", + "definitions": [ + { + "kind": "FragmentDefinition", + "name": { + "kind": "Name", + "value": "UserFragment" + }, + "directives": [], + "typeCondition": { + "kind": "NamedType", + "name": { + "kind": "Name", + "value": "User" + } + }, + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "id" + }, + "arguments": [], + "directives": [] + }, + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "name" + }, + "arguments": [], + "directives": [] + }, + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "address" + }, + "arguments": [], + "directives": [] + } + ] + } + } + ], + "loc": { + "start": 0, + "end": 46, + "source": { + "body": "fragment UserFragment on User{id name address}" + } + } +}; +const BABY_FRAGMENT = { + "kind": "Document", + "definitions": [ + { + "kind": "FragmentDefinition", + "name": { + "kind": "Name", + "value": "BabyFragment" + }, + "directives": [], + "typeCondition": { + "kind": "NamedType", + "name": { + "kind": "Name", + "value": "Baby" + } + }, + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "id" + }, + "arguments": [], + "directives": [] + }, + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "name" + }, + "arguments": [], + "directives": [] + }, + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "parentAddress" + }, + "arguments": [], + "directives": [] + } + ] + } + } + ], + "loc": { + "start": 0, + "end": 52, + "source": { + "body": "fragment BabyFragment on Baby{id name parentAddress}" + } + } +}; +const FRAGMENT_SPREAD_SELECTION = { + "kind": "Document", + "definitions": [ + { + "kind": "OperationDefinition", + "name": { + "kind": "Name", + "value": "testQuery" + }, + "directives": [], + "variableDefinitions": [], + "operation": "query", + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "getEntity" + }, + "arguments": [], + "directives": [], + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "FragmentSpread", + "name": { + "kind": "Name", + "value": "UserFragment" + }, + "directives": [] + }, + { + "kind": "FragmentSpread", + "name": { + "kind": "Name", + "value": "BabyFragment" + }, + "directives": [] + } + ] + } + } + ] + } + } + ], + "loc": { + "start": 0, + "end": 58, + "source": { + "body": "query testQuery{getEntity{...UserFragment...BabyFragment}}" + } + } +}; diff --git a/tests/fixtures/type/input.js b/tests/graphql_tag/type/input.js similarity index 100% rename from tests/fixtures/type/input.js rename to tests/graphql_tag/type/input.js diff --git a/tests/fixtures/type/output.js b/tests/graphql_tag/type/output.js similarity index 100% rename from tests/fixtures/type/output.js rename to tests/graphql_tag/type/output.js diff --git a/tests/graphql_tag/type/strip-output.js b/tests/graphql_tag/type/strip-output.js new file mode 100644 index 0000000..505ebf5 --- /dev/null +++ b/tests/graphql_tag/type/strip-output.js @@ -0,0 +1,237 @@ +import { gql } from "@apollo/client"; +const NAMED_TYPE = { + "kind": "Document", + "definitions": [ + { + "kind": "OperationDefinition", + "name": { + "kind": "Name", + "value": "testQuery" + }, + "directives": [], + "variableDefinitions": [ + { + "kind": "VariableDefinition", + "directives": [], + "variable": { + "kind": "Variable", + "name": { + "kind": "Name", + "value": "one" + } + }, + "type": { + "kind": "NamedType", + "name": { + "kind": "Name", + "value": "String" + } + }, + "defaultValue": { + "kind": "StringValue", + "value": "apple" + } + } + ], + "operation": "query", + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "getEntity" + }, + "arguments": [], + "directives": [] + } + ] + } + } + ], + "loc": { + "start": 0, + "end": 47, + "source": { + "body": 'query testQuery($one:String="apple"){getEntity}' + } + } +}; +const NAMED_TYPE_NOT_NULL = { + "kind": "Document", + "definitions": [ + { + "kind": "OperationDefinition", + "name": { + "kind": "Name", + "value": "testQuery" + }, + "directives": [], + "variableDefinitions": [ + { + "kind": "VariableDefinition", + "directives": [], + "variable": { + "kind": "Variable", + "name": { + "kind": "Name", + "value": "one" + } + }, + "type": { + "kind": "NonNullType", + "type": { + "kind": "NamedType", + "name": { + "kind": "Name", + "value": "String" + } + } + } + } + ], + "operation": "query", + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "getEntity" + }, + "arguments": [], + "directives": [] + } + ] + } + } + ], + "loc": { + "start": 0, + "end": 40, + "source": { + "body": "query testQuery($one:String!){getEntity}" + } + } +}; +const LIST_TYPE = { + "kind": "Document", + "definitions": [ + { + "kind": "OperationDefinition", + "name": { + "kind": "Name", + "value": "testQuery" + }, + "directives": [], + "variableDefinitions": [ + { + "kind": "VariableDefinition", + "directives": [], + "variable": { + "kind": "Variable", + "name": { + "kind": "Name", + "value": "one" + } + }, + "type": { + "kind": "ListType", + "type": { + "kind": "NamedType", + "name": { + "kind": "Name", + "value": "String" + } + } + } + } + ], + "operation": "query", + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "getEntity" + }, + "arguments": [], + "directives": [] + } + ] + } + } + ], + "loc": { + "start": 0, + "end": 41, + "source": { + "body": "query testQuery($one:[String]){getEntity}" + } + } +}; +const LIST_TYPE_NOT_NULL = { + "kind": "Document", + "definitions": [ + { + "kind": "OperationDefinition", + "name": { + "kind": "Name", + "value": "testQuery" + }, + "directives": [], + "variableDefinitions": [ + { + "kind": "VariableDefinition", + "directives": [], + "variable": { + "kind": "Variable", + "name": { + "kind": "Name", + "value": "one" + } + }, + "type": { + "kind": "ListType", + "type": { + "kind": "NonNullType", + "type": { + "kind": "NamedType", + "name": { + "kind": "Name", + "value": "String" + } + } + } + } + } + ], + "operation": "query", + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "getEntity" + }, + "arguments": [], + "directives": [] + } + ] + } + } + ], + "loc": { + "start": 0, + "end": 42, + "source": { + "body": "query testQuery($one:[String!]){getEntity}" + } + } +}; diff --git a/tests/fixtures/usage/defaultExport/input.js b/tests/graphql_tag/usage/defaultExport/input.js similarity index 100% rename from tests/fixtures/usage/defaultExport/input.js rename to tests/graphql_tag/usage/defaultExport/input.js diff --git a/tests/fixtures/usage/defaultExport/output.js b/tests/graphql_tag/usage/defaultExport/output.js similarity index 100% rename from tests/fixtures/usage/defaultExport/output.js rename to tests/graphql_tag/usage/defaultExport/output.js diff --git a/tests/graphql_tag/usage/defaultExport/strip-output.js b/tests/graphql_tag/usage/defaultExport/strip-output.js new file mode 100644 index 0000000..7b01f99 --- /dev/null +++ b/tests/graphql_tag/usage/defaultExport/strip-output.js @@ -0,0 +1,114 @@ +import { gql } from "@apollo/client"; +export default { + "kind": "Document", + "definitions": [ + { + "kind": "OperationDefinition", + "name": { + "kind": "Name", + "value": "testQuery" + }, + "directives": [], + "variableDefinitions": [ + { + "kind": "VariableDefinition", + "directives": [], + "variable": { + "kind": "Variable", + "name": { + "kind": "Name", + "value": "a" + } + }, + "type": { + "kind": "NonNullType", + "type": { + "kind": "NamedType", + "name": { + "kind": "Name", + "value": "String" + } + } + } + } + ], + "operation": "query", + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "testQueryName" + }, + "arguments": [ + { + "kind": "Argument", + "name": { + "kind": "Name", + "value": "a" + }, + "value": { + "kind": "Variable", + "name": { + "kind": "Name", + "value": "a" + } + } + } + ], + "directives": [ + { + "kind": "Directive", + "name": { + "kind": "Name", + "value": "apple" + } + } + ], + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "a" + }, + "arguments": [], + "directives": [] + }, + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "b" + }, + "arguments": [], + "directives": [] + }, + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "c" + }, + "arguments": [], + "directives": [] + } + ] + } + } + ] + } + } + ], + "loc": { + "start": 0, + "end": 61, + "source": { + "body": "query testQuery($a:String!){testQueryName(a:$a)@apple{a b c}}" + } + } +}; diff --git a/tests/fixtures/usage/returnFromArrowFunction/input.js b/tests/graphql_tag/usage/returnFromArrowFunction/input.js similarity index 100% rename from tests/fixtures/usage/returnFromArrowFunction/input.js rename to tests/graphql_tag/usage/returnFromArrowFunction/input.js diff --git a/tests/fixtures/usage/returnFromArrowFunction/output.js b/tests/graphql_tag/usage/returnFromArrowFunction/output.js similarity index 100% rename from tests/fixtures/usage/returnFromArrowFunction/output.js rename to tests/graphql_tag/usage/returnFromArrowFunction/output.js diff --git a/tests/graphql_tag/usage/returnFromArrowFunction/strip-output.js b/tests/graphql_tag/usage/returnFromArrowFunction/strip-output.js new file mode 100644 index 0000000..1614463 --- /dev/null +++ b/tests/graphql_tag/usage/returnFromArrowFunction/strip-output.js @@ -0,0 +1,114 @@ +import { gql } from "@apollo/client"; +const getQuery = ()=>({ + "kind": "Document", + "definitions": [ + { + "kind": "OperationDefinition", + "name": { + "kind": "Name", + "value": "testQuery" + }, + "directives": [], + "variableDefinitions": [ + { + "kind": "VariableDefinition", + "directives": [], + "variable": { + "kind": "Variable", + "name": { + "kind": "Name", + "value": "a" + } + }, + "type": { + "kind": "NonNullType", + "type": { + "kind": "NamedType", + "name": { + "kind": "Name", + "value": "String" + } + } + } + } + ], + "operation": "query", + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "testQueryName" + }, + "arguments": [ + { + "kind": "Argument", + "name": { + "kind": "Name", + "value": "a" + }, + "value": { + "kind": "Variable", + "name": { + "kind": "Name", + "value": "a" + } + } + } + ], + "directives": [ + { + "kind": "Directive", + "name": { + "kind": "Name", + "value": "apple" + } + } + ], + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "a" + }, + "arguments": [], + "directives": [] + }, + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "b" + }, + "arguments": [], + "directives": [] + }, + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "c" + }, + "arguments": [], + "directives": [] + } + ] + } + } + ] + } + } + ], + "loc": { + "start": 0, + "end": 61, + "source": { + "body": "query testQuery($a:String!){testQueryName(a:$a)@apple{a b c}}" + } + } + }); diff --git a/tests/fixtures/usage/returnFromFunctionExpression/input.js b/tests/graphql_tag/usage/returnFromFunctionExpression/input.js similarity index 100% rename from tests/fixtures/usage/returnFromFunctionExpression/input.js rename to tests/graphql_tag/usage/returnFromFunctionExpression/input.js diff --git a/tests/fixtures/usage/returnFromFunctionExpression/output.js b/tests/graphql_tag/usage/returnFromFunctionExpression/output.js similarity index 100% rename from tests/fixtures/usage/returnFromFunctionExpression/output.js rename to tests/graphql_tag/usage/returnFromFunctionExpression/output.js diff --git a/tests/graphql_tag/usage/returnFromFunctionExpression/strip-output.js b/tests/graphql_tag/usage/returnFromFunctionExpression/strip-output.js new file mode 100644 index 0000000..7dc95f1 --- /dev/null +++ b/tests/graphql_tag/usage/returnFromFunctionExpression/strip-output.js @@ -0,0 +1,116 @@ +import { gql } from "@apollo/client"; +const getQuery = function() { + return { + "kind": "Document", + "definitions": [ + { + "kind": "OperationDefinition", + "name": { + "kind": "Name", + "value": "testQuery" + }, + "directives": [], + "variableDefinitions": [ + { + "kind": "VariableDefinition", + "directives": [], + "variable": { + "kind": "Variable", + "name": { + "kind": "Name", + "value": "a" + } + }, + "type": { + "kind": "NonNullType", + "type": { + "kind": "NamedType", + "name": { + "kind": "Name", + "value": "String" + } + } + } + } + ], + "operation": "query", + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "testQueryName" + }, + "arguments": [ + { + "kind": "Argument", + "name": { + "kind": "Name", + "value": "a" + }, + "value": { + "kind": "Variable", + "name": { + "kind": "Name", + "value": "a" + } + } + } + ], + "directives": [ + { + "kind": "Directive", + "name": { + "kind": "Name", + "value": "apple" + } + } + ], + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "a" + }, + "arguments": [], + "directives": [] + }, + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "b" + }, + "arguments": [], + "directives": [] + }, + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "c" + }, + "arguments": [], + "directives": [] + } + ] + } + } + ] + } + } + ], + "loc": { + "start": 0, + "end": 61, + "source": { + "body": "query testQuery($a:String!){testQueryName(a:$a)@apple{a b c}}" + } + } + }; +}; diff --git a/tests/fixtures/values/input.js b/tests/graphql_tag/values/input.js similarity index 100% rename from tests/fixtures/values/input.js rename to tests/graphql_tag/values/input.js diff --git a/tests/fixtures/values/output.js b/tests/graphql_tag/values/output.js similarity index 100% rename from tests/fixtures/values/output.js rename to tests/graphql_tag/values/output.js diff --git a/tests/graphql_tag/values/strip-output.js b/tests/graphql_tag/values/strip-output.js new file mode 100644 index 0000000..ea12128 --- /dev/null +++ b/tests/graphql_tag/values/strip-output.js @@ -0,0 +1,264 @@ +import { gql } from "@apollo/client"; +const ALL_BASIC_VALUES = { + "kind": "Document", + "definitions": [ + { + "kind": "OperationDefinition", + "name": { + "kind": "Name", + "value": "testQuery" + }, + "directives": [], + "variableDefinitions": [ + { + "kind": "VariableDefinition", + "directives": [], + "variable": { + "kind": "Variable", + "name": { + "kind": "Name", + "value": "var" + } + }, + "type": { + "kind": "NamedType", + "name": { + "kind": "Name", + "value": "String" + } + } + } + ], + "operation": "query", + "selectionSet": { + "kind": "SelectionSet", + "selections": [ + { + "kind": "Field", + "name": { + "kind": "Name", + "value": "getEntity" + }, + "arguments": [ + { + "kind": "Argument", + "name": { + "kind": "Name", + "value": "var" + }, + "value": { + "kind": "Variable", + "name": { + "kind": "Name", + "value": "var" + } + } + }, + { + "kind": "Argument", + "name": { + "kind": "Name", + "value": "string" + }, + "value": { + "kind": "StringValue", + "value": "Hello" + } + }, + { + "kind": "Argument", + "name": { + "kind": "Name", + "value": "float" + }, + "value": { + "kind": "FloatValue", + "value": "2.2" + } + }, + { + "kind": "Argument", + "name": { + "kind": "Name", + "value": "int" + }, + "value": { + "kind": "IntValue", + "value": "1" + } + }, + { + "kind": "Argument", + "name": { + "kind": "Name", + "value": "bool" + }, + "value": { + "kind": "BooleanValue", + "value": true + } + }, + { + "kind": "Argument", + "name": { + "kind": "Name", + "value": "nullVal" + }, + "value": { + "kind": "NullValue" + } + }, + { + "kind": "Argument", + "name": { + "kind": "Name", + "value": "enumVal" + }, + "value": { + "kind": "EnumValue", + "value": "TestEnumValue" + } + }, + { + "kind": "Argument", + "name": { + "kind": "Name", + "value": "objectValue" + }, + "value": { + "kind": "ObjectValue", + "fields": [ + { + "kind": "ObjectField", + "name": { + "kind": "Name", + "value": "a" + }, + "value": { + "kind": "StringValue", + "value": "a" + } + }, + { + "kind": "ObjectField", + "name": { + "kind": "Name", + "value": "b" + }, + "value": { + "kind": "IntValue", + "value": "1" + } + }, + { + "kind": "ObjectField", + "name": { + "kind": "Name", + "value": "c" + }, + "value": { + "kind": "ObjectValue", + "fields": [ + { + "kind": "ObjectField", + "name": { + "kind": "Name", + "value": "a" + }, + "value": { + "kind": "FloatValue", + "value": "1.1" + } + }, + { + "kind": "ObjectField", + "name": { + "kind": "Name", + "value": "b" + }, + "value": { + "kind": "ListValue", + "values": [ + { + "kind": "IntValue", + "value": "1" + }, + { + "kind": "StringValue", + "value": "a" + } + ] + } + } + ] + } + } + ] + } + }, + { + "kind": "Argument", + "name": { + "kind": "Name", + "value": "list" + }, + "value": { + "kind": "ListValue", + "values": [ + { + "kind": "StringValue", + "value": "string" + }, + { + "kind": "FloatValue", + "value": "1.1" + }, + { + "kind": "IntValue", + "value": "1" + }, + { + "kind": "BooleanValue", + "value": true + }, + { + "kind": "NullValue" + }, + { + "kind": "EnumValue", + "value": "TestEnumValue" + }, + { + "kind": "ObjectValue", + "fields": [ + { + "kind": "ObjectField", + "name": { + "kind": "Name", + "value": "a" + }, + "value": { + "kind": "StringValue", + "value": "a" + } + } + ] + } + ] + } + } + ], + "directives": [] + } + ] + } + } + ], + "loc": { + "start": 0, + "end": 222, + "source": { + "body": 'query testQuery($var:String){getEntity(var:$var string:"Hello" float:2.2 int:1 bool:true nullVal:null enumVal:TestEnumValue objectValue:{a:"a",b:1,c:{a:1.1,b:[1,"a"]}}list:["string",1.1,1,true,null,TestEnumValue,{a:"a"}])}' + } + } +}; diff --git a/tests/unique_identifier/no_unique/input.js b/tests/unique_identifier/no_unique/input.js new file mode 100644 index 0000000..317a6b4 --- /dev/null +++ b/tests/unique_identifier/no_unique/input.js @@ -0,0 +1,14 @@ +import { gql } from "@apollo/client"; + +const QUERY = gql` + query testQuery { + getEntity { + id + name + } + } +`; + +const useTestQuery = () => { + return useQuery(QUERY); +}; diff --git a/tests/unique_identifier/no_unique/output.txt b/tests/unique_identifier/no_unique/output.txt new file mode 100644 index 0000000..1ebe04a --- /dev/null +++ b/tests/unique_identifier/no_unique/output.txt @@ -0,0 +1,2 @@ +identifier: unique +count: 0 diff --git a/tests/unique_identifier/with_unique_import/input.js b/tests/unique_identifier/with_unique_import/input.js new file mode 100644 index 0000000..81f2727 --- /dev/null +++ b/tests/unique_identifier/with_unique_import/input.js @@ -0,0 +1,15 @@ +import { gql } from "@apollo/client"; +import { unique } from "lol"; + +const QUERY = gql` + query testQuery { + getEntity { + id + name + } + } +`; + +const useTestQuery = () => { + return useQuery(QUERY); +}; diff --git a/tests/unique_identifier/with_unique_import/output.txt b/tests/unique_identifier/with_unique_import/output.txt new file mode 100644 index 0000000..a259263 --- /dev/null +++ b/tests/unique_identifier/with_unique_import/output.txt @@ -0,0 +1,2 @@ +identifier: unique +count: 1 diff --git a/tests/unique_identifier/with_unique_local/input.js b/tests/unique_identifier/with_unique_local/input.js new file mode 100644 index 0000000..c4a0927 --- /dev/null +++ b/tests/unique_identifier/with_unique_local/input.js @@ -0,0 +1,15 @@ +import { gql } from "@apollo/client"; + +const QUERY = gql` + query testQuery { + getEntity { + id + name + } + } +`; + +const useTestQuery = () => { + const unique = () => "unique"; + return useQuery(QUERY); +}; diff --git a/tests/unique_identifier/with_unique_local/output.txt b/tests/unique_identifier/with_unique_local/output.txt new file mode 100644 index 0000000..a259263 --- /dev/null +++ b/tests/unique_identifier/with_unique_local/output.txt @@ -0,0 +1,2 @@ +identifier: unique +count: 1