Skip to content
This repository has been archived by the owner on May 10, 2018. It is now read-only.

Commit

Permalink
test: added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrandwijk committed Dec 30, 2017
1 parent ce85963 commit 8f1e5ad
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { Generator } from './types'
export { Generator } from './types'

export function generateCode(schema: string, generator: Generator | string): string {
if (typeof generator === 'string'){
if (typeof generator === 'string') {
generator = generators[generator] || require(generator).generator
if (!generator) {
throw new Error(`Generator '${generator}' could not be found. Available generators:
${Object.keys(generators).map(k => `'${k}`).join(', ')}`)
}
}
}

const document: DocumentNode = parse(schema, { noLocation: true })

Expand Down
9 changes: 2 additions & 7 deletions test/createReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@ var options = {
jsonFile: 'test/report/cucumber_report.json',
output: 'test/report/cucumber_report.html',
reportSuiteAsScenarios: true,
launchReport: true,
launchReport: false,
metadata: {
"App Version":"0.3.2",
"Test Environment": "STAGING",
"Browser": "Chrome 54.0.2840.98",
"Platform": "Windows 10",
"Parallel": "Scenarios",
"Executed": "Remote"

}
};

Expand Down
10 changes: 8 additions & 2 deletions test/features/graphcool-ts.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Feature: Graphcool Typescript

Feature for Graphcool Typescript generator

Scenario: Scenario name
Scenario: Query type only
Given a schema looking like this:
"""
type Query {
Expand Down Expand Up @@ -49,4 +49,10 @@ Feature for Graphcool Typescript generator
posts: (args, info): Promise<Array<String> | null> => super.delegate('query', 'posts', args, {}, info)
}
}
"""
"""

Scenario: Query and Mutation type
Given the schema from 'test/testfiles/input/query-mutation.graphql'
And I pick generator 'graphcool-ts'
When I run the generator
Then I expect the output to match 'test/testfiles/output/query-mutation.ts'
15 changes: 13 additions & 2 deletions test/step_definitions/graphcool-ts.steps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as path from 'path'
import * as fs from 'fs';
import * as path from 'path';
import { defineSupportCode, TableDefinition, World } from 'cucumber'
import { generateCode } from '../../src'

Expand All @@ -7,18 +8,28 @@ defineSupportCode(function({ Given, When, Then }) {
this.schema = schema
})

Given(/^the schema from '(.*)'$/, function(filename) {
this.schema = fs.readFileSync(filename, 'utf-8')
})

Given('I pick generator {string}', function(generator) {
this.generator = generator
})

When('I run the generator', function() {
this.result = generateCode(this.schema, this.generator)
this.attach(this.result, 'application/typescript');
})

Then('I expect the output to be:', function(output) {
console.assert(normalizeText(this.result) == normalizeText(output), output)
})

Then(/^I expect the output to match '(.*)'$/, function(filename) {
const output = fs.readFileSync(filename, 'utf-8')
console.log(output)
console.log(this.result)
console.assert(normalizeText(this.result) == normalizeText(output), output)
})
})

function normalizeText(text) {
Expand Down
3 changes: 3 additions & 0 deletions test/testfiles/input/query-mutation.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type Query {
posts: [String]
}
36 changes: 36 additions & 0 deletions test/testfiles/output/query-mutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Graphcool as BaseGraphcool, BaseGraphcoolOptions } from 'graphcool-binding'
import { GraphQLResolveInfo } from 'graphql'

const typeDefs = `
type Query {
posts: [String]
}`

/*
The `Boolean` scalar type represents `true` or `false`.
*/
export type Boolean = boolean

/*
The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.
*/
export type String = string

export interface Schema {
query: Query
}

export type Query = {
posts: (args: {}, info?: GraphQLResolveInfo | string) => Promise<Array<String> | null>
}

export class Graphcool extends BaseGraphcool {

constructor({ endpoint, secret, fragmentReplacements, debug }: BaseGraphcoolOptions) {
super({ typeDefs, endpoint, secret, fragmentReplacements, debug });
}

query: Query = {
posts: (args, info): Promise<Array<String> | null> => super.delegate('query', 'posts', args, {}, info)
}
}

0 comments on commit 8f1e5ad

Please sign in to comment.