From ca0d938cdffb6e406ffe327b5b6e062afb73c67b Mon Sep 17 00:00:00 2001 From: IanLondon Date: Fri, 16 Aug 2019 13:37:23 -0400 Subject: [PATCH] fix(shared-data): fix "strict" arg for labware creation --- .../createIrregularLabware.test.js.snap | 3 +++ .../__snapshots__/createLabware.test.js.snap | 3 +++ .../__tests__/createIrregularLabware.test.js | 20 ++++++++++++++++--- .../__tests__/createLabware.test.js | 17 ++++++++++++++-- shared-data/js/labwareTools/index.js | 6 +++--- 5 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 shared-data/js/labwareTools/__tests__/__snapshots__/createIrregularLabware.test.js.snap create mode 100644 shared-data/js/labwareTools/__tests__/__snapshots__/createLabware.test.js.snap diff --git a/shared-data/js/labwareTools/__tests__/__snapshots__/createIrregularLabware.test.js.snap b/shared-data/js/labwareTools/__tests__/__snapshots__/createIrregularLabware.test.js.snap new file mode 100644 index 00000000000..99a8acd70e8 --- /dev/null +++ b/shared-data/js/labwareTools/__tests__/__snapshots__/createIrregularLabware.test.js.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`test createIrregularLabware function failing to validate against labware schema throws w/o "strict" 1`] = `"Generated labware failed to validate, please check your inputs"`; diff --git a/shared-data/js/labwareTools/__tests__/__snapshots__/createLabware.test.js.snap b/shared-data/js/labwareTools/__tests__/__snapshots__/createLabware.test.js.snap new file mode 100644 index 00000000000..4022be49725 --- /dev/null +++ b/shared-data/js/labwareTools/__tests__/__snapshots__/createLabware.test.js.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`createLabware failing to validate against labware schema throws w/o "strict" 1`] = `"Generated labware failed to validate, please check your inputs"`; diff --git a/shared-data/js/labwareTools/__tests__/createIrregularLabware.test.js b/shared-data/js/labwareTools/__tests__/createIrregularLabware.test.js index a9ae42203b6..a0ed8514ea5 100644 --- a/shared-data/js/labwareTools/__tests__/createIrregularLabware.test.js +++ b/shared-data/js/labwareTools/__tests__/createIrregularLabware.test.js @@ -84,9 +84,9 @@ describe('test helper functions', () => { describe('test createIrregularLabware function', () => { let labware1 - + let labware1Args beforeEach(() => { - labware1 = createIrregularLabware({ + labware1Args = { namespace: 'fixture', metadata: { displayName: 'Fake Irregular Container', @@ -125,7 +125,8 @@ describe('test createIrregularLabware function', () => { { rowStart: 'A', colStart: '1', rowStride: 2, colStride: 1 }, { rowStart: 'B', colStart: '1', rowStride: 1, colStride: 1 }, ], - }) + } + labware1 = createIrregularLabware(labware1Args) }) test('irregular ordering generates as expected', () => { @@ -174,4 +175,17 @@ describe('test createIrregularLabware function', () => { expect(loadName).toEqual('somebrand_6_wellplate_6x4ml') }) + + test('failing to validate against labware schema throws w/o "strict"', () => { + const args = { + ...labware1Args, + // negative y offset should fail schema validation by making well `y` negative + offset: [{ x: 10, y: -9999, z: 69.48 }, { x: 15, y: -9999, z: 69.48 }], + } + + expect(() => createIrregularLabware(args)).toThrowErrorMatchingSnapshot() + expect(() => + createIrregularLabware({ ...args, strict: false }) + ).not.toThrow() + }) }) diff --git a/shared-data/js/labwareTools/__tests__/createLabware.test.js b/shared-data/js/labwareTools/__tests__/createLabware.test.js index ebb0ae7120a..1f179b095a3 100644 --- a/shared-data/js/labwareTools/__tests__/createLabware.test.js +++ b/shared-data/js/labwareTools/__tests__/createLabware.test.js @@ -26,6 +26,7 @@ const exampleLabware2 = { describe('createLabware', () => { let labware1 let labware2 + let labware2Args let well1 let well2 let offset1 @@ -49,7 +50,7 @@ describe('createLabware', () => { namespace: 'fixture', }) - labware2 = createRegularLabware({ + labware2Args = { metadata: exampleLabware2.metadata, parameters: exampleLabware2.parameters, dimensions: exampleLabware2.dimensions, @@ -58,7 +59,8 @@ describe('createLabware', () => { spacing: { row: 10, column: 10 }, well: well2, namespace: 'fixture', - }) + } + labware2 = createRegularLabware(labware2Args) }) afterEach(() => { @@ -110,4 +112,15 @@ describe('createLabware', () => { }) }) }) + + test('failing to validate against labware schema throws w/o "strict"', () => { + const args = { + ...labware2Args, + // this spacing should make negative well `y` value and fail schema validation + spacing: { row: 999, column: 999 }, + } + + expect(() => createRegularLabware(args)).toThrowErrorMatchingSnapshot() + expect(() => createRegularLabware({ ...args, strict: false })).not.toThrow() + }) }) diff --git a/shared-data/js/labwareTools/index.js b/shared-data/js/labwareTools/index.js index ae5e2130cd1..2b53ec504e6 100644 --- a/shared-data/js/labwareTools/index.js +++ b/shared-data/js/labwareTools/index.js @@ -87,7 +87,7 @@ const validate = ajv.compile(labwareSchema) function validateDefinition( definition: Definition, - strict: boolean = true + strict: ?boolean = true ): Definition { const valid = validate(definition) @@ -328,7 +328,7 @@ export function createDefaultDisplayName(args: RegularNameProps): string { // or the labware definition schema in labware/schemas/ export function createRegularLabware(args: RegularLabwareProps): Definition { const { offset, dimensions, grid, spacing, well, loadNamePostfix } = args - const strict = args.strict || true + const strict = args.strict const version = args.version || 1 const namespace = args.namespace || DEFAULT_CUSTOM_NAMESPACE const ordering = determineOrdering(grid) @@ -372,7 +372,7 @@ export function createIrregularLabware( args: IrregularLabwareProps ): Definition { const { offset, dimensions, grid, spacing, well, gridStart, group } = args - const strict = args.strict || true + const strict = args.strict const namespace = args.namespace || DEFAULT_CUSTOM_NAMESPACE const version = args.version || 1 const { wells, groups } = determineIrregularLayout(