diff --git a/lib/index.js b/lib/index.js index 2acaf91..3d88f82 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1017,6 +1017,7 @@ const createTestFile = async (action, operation) => { const zapier = require('zapier-platform-core'); const { getConfig } = require('zapier-graphql'); +const { assertTypes } = require('zapier-graphql/lib/utils'); const App = require('../../index'); const appTester = zapier.createAppTester(App); @@ -1031,32 +1032,6 @@ describe('${definition.name} ${queryOrMutation}', () => { inputData: ${indentString(JSON.stringify(samples, null, 2), 6)}, }; - const assertTypes = (sample, object) => { - for (const [key, value] of Object.entries(object)) { - const field = sample[key]; - expect(field).toBeDefined(); - - // We don't have an easy way to run any assertions against null fields - if (value === null) { - continue; - } - - if (field.children) { - expect(typeof value).toBe('object'); - assertTypes(field.children, value); - continue; - } - - if (field.type === 'datetime') { - expect(typeof value).toBe('string'); - } else if (field.type === 'integer') { - expect(value % 1 === 0).toBe(true); - } else { - expect(typeof value).toBe(typeof field); - } - } - } - let results = await appTester(App.${inflection.pluralize(action)}.${operation}.operation.perform, bundle); results = results instanceof Array ? results : [results]; expect(results.length).toBeGreaterThan(0); diff --git a/lib/utils/index.js b/lib/utils/index.js index 6d21c16..f0a0b31 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -15,7 +15,40 @@ const quote = (value) => { return value; } +/** + * Asserts that the types of the object match the sample object's defined types + * + * @param {Object} sample + * @param {Object} object + */ +const assertTypes = (sample, object) => { + for (const [key, value] of Object.entries(object)) { + const field = sample[key]; + expect(field).toBeDefined(); + + // We don't have an easy way to run any assertions against null fields + if (value === null) { + continue; + } + + if (field.children) { + expect(typeof value).toBe('object'); + assertTypes(field.children, value); + continue; + } + + if (field.type === 'datetime') { + expect(typeof value).toBe('string'); + } else if (field.type === 'integer') { + expect(value % 1 === 0).toBe(true); + } else { + expect(typeof value).toBe(typeof field); + } + } +} + module.exports = { quote, + assertTypes, };