diff --git a/lib/mockBuilder.js b/lib/mockBuilder.js index aeffaaf..35f9b91 100644 --- a/lib/mockBuilder.js +++ b/lib/mockBuilder.js @@ -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 @@ -17,6 +17,7 @@ function mockBuilder (schemaCode, customMock) { mockSchema[type] = mockField } } + clearMemoizedFields() return mockSchema } diff --git a/test/multiplesSchemas.js b/test/multiplesSchemas.js new file mode 100644 index 0000000..a3e0ce2 --- /dev/null +++ b/test/multiplesSchemas.js @@ -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') + }) +}) diff --git a/util/index.js b/util/index.js index 0944408..44a4b31 100644 --- a/util/index.js +++ b/util/index.js @@ -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] @@ -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. @@ -145,4 +149,4 @@ function getUnionVals (field, schema) { return schema[field.type].types } -module.exports = memoizedField +module.exports = { memoizedField, clearMemoizedFields }