Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/mockBuilder.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const schemaParser = require('easygraphql-parser')
const memoizedField = require('../util')
const { memoizedField, clearMemoizedFields } = require('../util')

function mockBuilder (schemaCode, customMock) {
// Parse the schema on the GQL file to create a cutom object easier to loop
Expand All @@ -17,6 +17,7 @@ function mockBuilder (schemaCode, customMock) {
mockSchema[type] = mockField
}
}
clearMemoizedFields()

return mockSchema
}
Expand Down
41 changes: 41 additions & 0 deletions test/multiplesSchemas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* eslint-env mocha */
/* eslint-disable no-unused-expressions */

'use strict'

const { expect } = require('chai')
const easygraphqlMock = require('../lib/mockBuilder')

describe('#Multilples schemas', () => {
const schema1 = `
type Author {
name: String!
}
`

const schema2 = `
type Author {
title: String!
favoriteProject: Project!
}

type Project {
name: String!
}
`

it('Should return schema1 info', () => {
const mockedSchema1 = easygraphqlMock(schema1)

expect(mockedSchema1.Author.name).to.be.a('string')
})

it('Should return schema2 info', () => {
const mockedSchema2 = easygraphqlMock(schema2)

expect(mockedSchema2.Author.title).to.be.a('string')
expect(mockedSchema2.Author.favoriteProject).to.be.an('object')
expect(mockedSchema2.Author.favoriteProject.name).to.be.a('string')
expect(mockedSchema2.Project.name).to.be.a('string')
})
})
8 changes: 6 additions & 2 deletions util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const randomNumberData = require('./randomData/number')
const randomBooleanData = require('./randomData/boolean')
const { randomNumber } = require('./utils')

let cache = {}
const memoize = (fn) => {
const cache = {}
return (type, customMock, schema) => {
if (type in cache) {
return cache[type]
Expand All @@ -27,6 +27,10 @@ const memoize = (fn) => {
}
}

const clearMemoizedFields = () => {
cache = {}
}

const mockedField = (type, customMock, schema) => {
// This will be the result of each field inside the type, the key is going to
// be the same name that is set inside the type.
Expand Down Expand Up @@ -145,4 +149,4 @@ function getUnionVals (field, schema) {
return schema[field.type].types
}

module.exports = memoizedField
module.exports = { memoizedField, clearMemoizedFields }