Skip to content

Commit

Permalink
Move assertTypes to utils helper
Browse files Browse the repository at this point in the history
  • Loading branch information
oojacoboo committed Feb 9, 2024
1 parent 26d3d3c commit 80deed2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
27 changes: 1 addition & 26 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
33 changes: 33 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

0 comments on commit 80deed2

Please sign in to comment.