Skip to content

Commit

Permalink
chore: improve tests
Browse files Browse the repository at this point in the history
Merge pull request #15 from rishabh3112/chore/improve-tests
  • Loading branch information
rishabh3112 authored Dec 15, 2023
2 parents 18a4de3 + bb12de5 commit 5fe96bf
Show file tree
Hide file tree
Showing 43 changed files with 1,919 additions and 5 deletions.
67 changes: 62 additions & 5 deletions tests/fixtures.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -17,19 +18,21 @@ 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| {
as_folder(TransformVisitor::new(
GraphQLTagConfig {
import_sources: vec!["@apollo/client".to_string(), "graphql-tag".into()],
gql_tag_identifiers: vec!["gql".to_string()],
strip: true,
strip: false,
unique_fn_name: "unique".into(),
unique_fn_used: false,
},
Expand All @@ -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<String, std::io::Error> = 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();
}
}
51 changes: 51 additions & 0 deletions tests/graphql_tag/definations/fragment/output.js
Original file line number Diff line number Diff line change
@@ -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": 57,
"source": {
"body": "\n fragment TestFragment on Entity {\n id\n name\n }\n"
}
}
};
71 changes: 71 additions & 0 deletions tests/graphql_tag/definations/operation/mutation/output.js
Original file line number Diff line number Diff line change
@@ -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": 82,
"source": {
"body": "\n mutation testMutation($entity: String) {\n updateEntity(entity: $entity)\n }\n"
}
}
};
60 changes: 60 additions & 0 deletions tests/graphql_tag/definations/operation/query/output.js
Original file line number Diff line number Diff line change
@@ -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": 67,
"source": {
"body": "\n query testQuery {\n getEntity {\n id\n name\n }\n }\n"
}
}
};
File renamed without changes.
Loading

0 comments on commit 5fe96bf

Please sign in to comment.