Skip to content

Commit

Permalink
adding test
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Martinez Pupo committed Feb 26, 2020
1 parent c579a69 commit 3c1e400
Show file tree
Hide file tree
Showing 13 changed files with 1,616 additions and 5 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ typings/
config/
resolvers/
types/
test/

node_modules

Expand Down
1 change: 0 additions & 1 deletion helpers/schema-loader.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const { createSchema } = require('swagger-to-graphql')
const callBackend = require('./callbackend')
const request = require('request-promise')
const url = require('url')

/**
* @param {GraqhQLSchema} localSchema
Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@
"helpers"
],
"scripts": {
"format": "npx standard --fix",
"lint:fix": "npx standard --fix",
"lint": "npx standard",
"test": "jest --coverage --detectOpenHandles --runInBand --testRegex=test/.*\\.spec\\.js --testPathIgnorePatterns=test/.*.snap"
"test": "jest --coverage --detectOpenHandles --runInBand --testRegex=test/.*\\.spec\\.js --testPathIgnorePatterns=test/mocks/*.js"
},
"version": "1.0.4"
"standard": {
"env": [ "jest" ]
},
"version": "1.0.5"
}
72 changes: 72 additions & 0 deletions test/aggregation-layer.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const { createTestClient } = require('apollo-server-testing')
const gql = require('graphql-tag')
const gateway = require('./../index')
const { reservations } = require('./mocks/data/mocks')
require('./mocks/gql-gateway-requests')

describe('agreggation layer', () => {
let Query = null

const endpointsList = [
{ name: 'reservation_service', url: 'http://localhost/reservation-api/swagger.json' },
{ name: 'client_service', url: 'http://localhost/client-api/swagger.json' }
]

const localSchema = `
extend type Client {
reservations : Reservations
}
`

const GET_CLIENTS = gql`
query get_clients {
get_clients {
clientId,
firstName,
middleName,
reservations {
data {
reservationId,
location
},
totalCount
}
}
}
`

const resolvers = {
Client: {
reservations: {
fragment: '... on Client {clientId}',
async resolve (client, args, context, info) {
const schema = await context.resolveSchema('reservation_service')

return info.mergeInfo.delegateToSchema({
schema,
operation: 'query',
fieldName: 'get_reservations_client_clientId',
args: { clientId: client.clientId },
context,
info
})
}
}
}
}

beforeAll(async () => {
const server = await gateway({ localSchema, resolvers, endpointsList })
server.context = server.context({ req: { headers: { authorization: 'Bearer XXXXX' } } })
const { query } = createTestClient(server)

Query = query
})

it('load clients with aggregations', async () => {
const { data } = await Query({ query: GET_CLIENTS })

expect(data.get_clients.length).toBe(2)
expect(data.get_clients[0].reservations.data).toEqual(reservations)
})
})
27 changes: 27 additions & 0 deletions test/construct.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const gateway = require('../index')
const apolloServer = require('apollo-server')

describe('Server construct', () => {
it('error on empty swagger endpoints list', async () => {
try {
await gateway({})
} catch (err) {
expect(err.message).toBe('The parameter endpointsList is required')
}
})

it('error loading endpoints', async () => {
try {
const endpointsList = [{ name: 'petstore_service', url: 'https://no-existent-endpoint/swagger.json' }]
await gateway({ endpointsList })
} catch (err) {
expect(err.message).toBe('Error: getaddrinfo ENOTFOUND no-existent-endpoint no-existent-endpoint:443')
}
})

it('load of PetStore schema', async () => {
const endpointsList = [{ name: 'petstore_service', url: 'https://petstore.swagger.io/v2/swagger.json' }]
const server = await gateway({ endpointsList })
expect(server instanceof apolloServer.ApolloServer)
})
})
15 changes: 15 additions & 0 deletions test/mocks/data/mocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
clients: [{
clientId: 1,
firstName: 'Ivan',
middleName: 'Martinez'
}, {
clientId: 2,
firstName: 'George',
middleName: 'Schmith'
}],
reservations: [{
reservationId: 12321,
location: 'My remote location'
}]
}
111 changes: 111 additions & 0 deletions test/mocks/data/swagger-client-api.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
{
"swagger": "2.0",
"info": {
"description": "Sample Client Api",
"version": "1.0.0",
"title": "Clients API"
},
"host" : "localhost",
"basePath": "/client-api",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
{
"name": "client",
"description": "Client"
}
],
"schemes": [
"http"
],
"definitions": {
"Client": {
"type": "object",
"properties": {
"clientId": {
"type": "string",
"readOnly": true
},
"firstName": {
"type": "string"
},
"middleName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"active": {
"type": "boolean"
},
"salutation": {
"type": "string"
},
"address": {
"type": "string"
},
"email": {
"type": "string"
}
}
}
},
"paths": {
"/clients": {
"get": {
"tags": [
"client"
],
"summary": "Get Clients",
"description": "Get all clients",
"responses": {
"200": {
"description": "successful operation",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/Client"
}
}
},
"400": {
"description": "Bad Request"
}
}
}
},
"/clients/{clientId}": {
"get": {
"tags": [
"client"
],
"summary": "Find a client by ID",
"description": "",
"parameters": [
{
"name": "clientId",
"in": "path",
"description": "ID of the client to be fetched",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "successful operation",
"schema": {
"$ref": "#/definitions/Client"
}
},
"404": {
"description": "Not Found"
}
}
}
}
}
}
Loading

0 comments on commit 3c1e400

Please sign in to comment.