Skip to content

Commit

Permalink
Split assertType into 2 functions based on what data we're using for …
Browse files Browse the repository at this point in the history
…these assertions
  • Loading branch information
oojacoboo committed Feb 10, 2024
1 parent 80deed2 commit c495cff
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +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 { assertTypesFromSample } = require('zapier-graphql/lib/utils');
const App = require('../../index');
const appTester = zapier.createAppTester(App);
Expand All @@ -1039,7 +1039,7 @@ describe('${definition.name} ${queryOrMutation}', () => {
const sample = App.${inflection.pluralize(action)}.${operation}.operation.sample
expect(Object.keys(firstResult).length).toEqual(Object.keys(sample).length);
assertTypes(sample, firstResult);
assertTypesFromSample(sample, firstResult);
});
});
`;
Expand Down
40 changes: 37 additions & 3 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const quote = (value) => {
* @param {Object} sample
* @param {Object} object
*/
const assertTypes = (sample, object) => {
const assertTypesFromSample = (sample, object) => {
for (const [key, value] of Object.entries(object)) {
const field = sample[key];
expect(field).toBeDefined();
Expand All @@ -33,7 +33,7 @@ const assertTypes = (sample, object) => {

if (field.children) {
expect(typeof value).toBe('object');
assertTypes(field.children, value);
assertTypesFromSample(field.children, value);
continue;
}

Expand All @@ -48,7 +48,41 @@ const assertTypes = (sample, object) => {
}


/**
* Asserts that the types of the object match the type defined in the output fields
*
* @param {Array} fields
* @param {Object} object
*/
const assertTypesFromOutputFields = (fields, object) => {
for (const [key, value] of Object.entries(object)) {
const field = fields.find(field => field.key === 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');
assertTypesFromOutputFields(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(field.type);
}
}
}


module.exports = {
quote,
assertTypes,
assertTypesFromSample,
assertTypesFromOutputFields,
};

0 comments on commit c495cff

Please sign in to comment.