From 7e1f352c7000f8aadd4bdbe3df3aa913b0bcc594 Mon Sep 17 00:00:00 2001 From: Elisa Dinsmore Date: Thu, 17 Nov 2022 10:17:18 -0500 Subject: [PATCH 01/11] example data hook --- .../FieldHooks/ComputeFieldHooks/removeExtraSpaces.ts | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 examples/FieldHooks/ComputeFieldHooks/removeExtraSpaces.ts diff --git a/examples/FieldHooks/ComputeFieldHooks/removeExtraSpaces.ts b/examples/FieldHooks/ComputeFieldHooks/removeExtraSpaces.ts new file mode 100644 index 0000000..519516e --- /dev/null +++ b/examples/FieldHooks/ComputeFieldHooks/removeExtraSpaces.ts @@ -0,0 +1,9 @@ +import { TextField, Sheet } from '@flatfile/configure' + +const removeExtraSpaces = new Sheet('removeExtraSpaces', { + departmentName: TextField({ + compute: (value: any) => { + return value.replace(/\s{2,}/g, ' ') + }, + }), +}) From cbde0095c6ac1929bb4b8c1f412a883ba1f24f1a Mon Sep 17 00:00:00 2001 From: Elisa Dinsmore Date: Thu, 17 Nov 2022 13:37:40 -0500 Subject: [PATCH 02/11] added tests --- .../ComputeFieldHooks/removeExtraSpaces.ts | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/examples/FieldHooks/ComputeFieldHooks/removeExtraSpaces.ts b/examples/FieldHooks/ComputeFieldHooks/removeExtraSpaces.ts index 519516e..9b10be1 100644 --- a/examples/FieldHooks/ComputeFieldHooks/removeExtraSpaces.ts +++ b/examples/FieldHooks/ComputeFieldHooks/removeExtraSpaces.ts @@ -1,4 +1,5 @@ -import { TextField, Sheet } from '@flatfile/configure' +import { TextField, Sheet, Workbook } from '@flatfile/configure' +import { SheetTester } from '../../../src/utils/testing/SheetTester' const removeExtraSpaces = new Sheet('removeExtraSpaces', { departmentName: TextField({ @@ -7,3 +8,33 @@ const removeExtraSpaces = new Sheet('removeExtraSpaces', { }, }), }) + +const TestWorkbook = new Workbook({ + name: 'Test Workbook', + namespace: 'Test', + sheets: { removeExtraSpaces }, +}) + +describe('Workbook tests ->', () => { + // here we use Sheet tester + const testSheet = new SheetTester(TestWorkbook, 'SubSheet') + test('Remove Extra Spaces', async () => { + // for this inputRow + const inputRow = { departmentName: ' asdf' } + // we expect this output row + const expectedOutputRow = { numField: ' asdf' } + const res = await testSheet.testRecord(inputRow) + //call the expectation here + expect(res).toMatchObject(expectedOutputRow) + }) + + // test('Convert to spanish number word works', async () => { + // const inputRow = { numField: 'un'} + // const expectedOutputRow = { numField: 'un'} + // const res = await testSheet.testRecord(inputRow) + // expect(res).toMatchObject(expectedOutputRow) + + // const res2 = await testSheet.testRecord({ numField: 'two'}) + // expect(res2).toMatchObject({ numField: 'dos' }) + // }) +}) From 16be29d1c6e11647161636f04249c7b3fac28799 Mon Sep 17 00:00:00 2001 From: Elisa Dinsmore Date: Thu, 17 Nov 2022 13:38:59 -0500 Subject: [PATCH 03/11] updated SheetTester --- jest.config.js | 2 +- src/utils/testing/SheetTester.ts | 61 +++++++++++++++++++++++++++++--- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/jest.config.js b/jest.config.js index 997df40..f5c3e51 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,5 +1,5 @@ module.exports = { - roots: ['/src'], + roots: ['/src', '/examples'], testEnvironment: 'node', transform: { '^.+\\.tsx?$': 'ts-jest', diff --git a/src/utils/testing/SheetTester.ts b/src/utils/testing/SheetTester.ts index e30aefb..6f3acb9 100644 --- a/src/utils/testing/SheetTester.ts +++ b/src/utils/testing/SheetTester.ts @@ -1,6 +1,5 @@ import _ from 'lodash' - -import { FlatfileRecords, FlatfileSession, IPayload } from '@flatfile/hooks' +import { IRecordInfo, TRecordData, TPrimitive, FlatfileRecords, FlatfileSession, IPayload } from '@flatfile/hooks' import { Workbook } from '@flatfile/configure' export class SheetTester { @@ -97,14 +96,66 @@ export class SheetTester { return transform(pkey, value) } - public async testRecord(recordBatch: {}) { - const transformedRecords = await this.transformRecords([recordBatch]) + public async testRecord(record: {}) { + const transformedRecords = await this.transformRecords([record]) return transformedRecords.records[0].value } - public async testRecords(recordBatch: any[]) { + public async testRecords(recordBatch: Record[]) { const transformedRecords = await this.transformRecords(recordBatch) return transformedRecords.records.map((r) => r.value) } + public async testMessage(record: {}) { + const transformedRecords = await this.transformRecords([record]) + return transformedRecords.records.map((r) => r.toJSON().info)[0] + } + public async testMessages(recordBatch: Record[]) { + const transformedRecords = await this.transformRecords(recordBatch) + return transformedRecords.records.map((r) => r.toJSON().info) + } + +} + +// export interface InfoObj { +// field: string +// message: string +// level: TRecordStageLevel +// stage: 'validate' | 'compute' +// } + +export type InfoObj = IRecordInfo, string | number> + +export const removeUndefineds = (obj:Record) => _.pickBy(obj, _.identity) +export const matchMessages = (messages:InfoObj[], field?:string, message?:string, level?:string): false| any[] => { + + const results = _.filter(messages, removeUndefineds({field,message,level})) + if (results.length > 0) { + return results + } + return false } + +export const matchSingleMessage = ( + messages:InfoObj[], field?:string, message?:string, level?:string): false| any => { + const results = matchMessages(messages, field, message, level) + if (results === false) { + return false + } + if (results.length === 1) { + return results[0] + } + if (results.length > 1) { + throw new Error("more than one message returned") + } + if (results.length === 0) { + //unreachable + return false + } + //unreachable + return false +} + +//use the match functions like +// const res = await testSheet.testMessage(inputRow) +// expect(matchSingleMessage(res, 'numField', 'more than 5', 'error')).toBeTruthy() From efd49835a975f2900fa7a3f4836cf9b81f04ab49 Mon Sep 17 00:00:00 2001 From: Elisa Dinsmore Date: Mon, 21 Nov 2022 16:05:55 -0500 Subject: [PATCH 04/11] update removeExtraSpaces file --- .../removeExtraSpaces.spec.ts | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 examples/FieldHooks/ComputeFieldHooks/removeExtraSpaces.spec.ts diff --git a/examples/FieldHooks/ComputeFieldHooks/removeExtraSpaces.spec.ts b/examples/FieldHooks/ComputeFieldHooks/removeExtraSpaces.spec.ts new file mode 100644 index 0000000..e25f90e --- /dev/null +++ b/examples/FieldHooks/ComputeFieldHooks/removeExtraSpaces.spec.ts @@ -0,0 +1,61 @@ +import { TextField, Sheet, Workbook } from '@flatfile/configure' +import { SheetTester } from '../../../src/utils/testing/SheetTester' + +const removeExtraSpaces = new Sheet('removeExtraSpaces', { + //here we set up a field + departmentName: TextField({ + //we define a compute hook + compute: (value: any) => { + //matches two or more whitespace characters and replaces them with one space + return value.replace(/\s{2,}/g, ' ') + }, + }), +}) + +const TestWorkbook = new Workbook({ + name: 'Test Workbook', + namespace: 'Test', + sheets: { removeExtraSpaces }, +}) + +describe('Workbook tests ->', () => { + // here we use Sheet tester + const testSheet = new SheetTester(TestWorkbook, 'removeExtraSpaces') + test('Remove Extra Leading Spaces', async () => { + // for this inputRow + const inputRow = { departmentName: ' asdf' } + // we expect this output row + const expectedOutputRow = { departmentName: ' asdf' } + const res = await testSheet.testRecord(inputRow) + //call the expectation here + expect(res).toMatchObject(expectedOutputRow) + }) + + test('Test set of numbers', async () => { + const inputRow = { departmentName: '123456789123456' } + const expectedOutputRow = { departmentName: '123456789123456' } + const res = await testSheet.testRecord(inputRow) + expect(res).toMatchObject(expectedOutputRow) + }) + + test('Test trailing spaces', async () => { + const inputRow = { departmentName: 'asdf ' } + const expectedOutputRow = { departmentName: 'asdf ' } + const res = await testSheet.testRecord(inputRow) + expect(res).toMatchObject(expectedOutputRow) + }) + + test('Test multiple instances of extra spaces', async () => { + const inputRow = { departmentName: ' as df' } + const expectedOutputRow = { departmentName: ' as df' } + const res = await testSheet.testRecord(inputRow) + expect(res).toMatchObject(expectedOutputRow) + }) + + test('Test set of numbers', async () => { + const inputRow = { departmentName: 'Paddy Mullen' } + const expectedOutputRow = { departmentName: 'Paddy Mullen' } + const res = await testSheet.testRecord(inputRow) + expect(res).toMatchObject(expectedOutputRow) + }) +}) From c3d9fd644b117f4d8e6f0d2be9bbc41d61853b86 Mon Sep 17 00:00:00 2001 From: Elisa Dinsmore Date: Mon, 21 Nov 2022 16:08:23 -0500 Subject: [PATCH 05/11] remove old file --- .../ComputeFieldHooks/removeExtraSpaces.ts | 40 ------------------- 1 file changed, 40 deletions(-) delete mode 100644 examples/FieldHooks/ComputeFieldHooks/removeExtraSpaces.ts diff --git a/examples/FieldHooks/ComputeFieldHooks/removeExtraSpaces.ts b/examples/FieldHooks/ComputeFieldHooks/removeExtraSpaces.ts deleted file mode 100644 index 9b10be1..0000000 --- a/examples/FieldHooks/ComputeFieldHooks/removeExtraSpaces.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { TextField, Sheet, Workbook } from '@flatfile/configure' -import { SheetTester } from '../../../src/utils/testing/SheetTester' - -const removeExtraSpaces = new Sheet('removeExtraSpaces', { - departmentName: TextField({ - compute: (value: any) => { - return value.replace(/\s{2,}/g, ' ') - }, - }), -}) - -const TestWorkbook = new Workbook({ - name: 'Test Workbook', - namespace: 'Test', - sheets: { removeExtraSpaces }, -}) - -describe('Workbook tests ->', () => { - // here we use Sheet tester - const testSheet = new SheetTester(TestWorkbook, 'SubSheet') - test('Remove Extra Spaces', async () => { - // for this inputRow - const inputRow = { departmentName: ' asdf' } - // we expect this output row - const expectedOutputRow = { numField: ' asdf' } - const res = await testSheet.testRecord(inputRow) - //call the expectation here - expect(res).toMatchObject(expectedOutputRow) - }) - - // test('Convert to spanish number word works', async () => { - // const inputRow = { numField: 'un'} - // const expectedOutputRow = { numField: 'un'} - // const res = await testSheet.testRecord(inputRow) - // expect(res).toMatchObject(expectedOutputRow) - - // const res2 = await testSheet.testRecord({ numField: 'two'}) - // expect(res2).toMatchObject({ numField: 'dos' }) - // }) -}) From 81ceaee87167c836554cb9ce68367d64f771555b Mon Sep 17 00:00:00 2001 From: Elisa Dinsmore Date: Mon, 21 Nov 2022 16:09:41 -0500 Subject: [PATCH 06/11] added comments --- examples/FieldHooks/ComputeFieldHooks/removeExtraSpaces.spec.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/FieldHooks/ComputeFieldHooks/removeExtraSpaces.spec.ts b/examples/FieldHooks/ComputeFieldHooks/removeExtraSpaces.spec.ts index e25f90e..7aab1a0 100644 --- a/examples/FieldHooks/ComputeFieldHooks/removeExtraSpaces.spec.ts +++ b/examples/FieldHooks/ComputeFieldHooks/removeExtraSpaces.spec.ts @@ -31,6 +31,7 @@ describe('Workbook tests ->', () => { expect(res).toMatchObject(expectedOutputRow) }) + //additional tests for additional cases test('Test set of numbers', async () => { const inputRow = { departmentName: '123456789123456' } const expectedOutputRow = { departmentName: '123456789123456' } From a4001f51cff7a65a4944f676d00780cf66636c99 Mon Sep 17 00:00:00 2001 From: Elisa Dinsmore Date: Mon, 21 Nov 2022 16:18:57 -0500 Subject: [PATCH 07/11] New Hook Example --- .../RemoveSymbolsCompute.spec.ts | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 examples/FieldHooks/ComputeFieldHooks/RemoveSymbolsCompute.spec.ts diff --git a/examples/FieldHooks/ComputeFieldHooks/RemoveSymbolsCompute.spec.ts b/examples/FieldHooks/ComputeFieldHooks/RemoveSymbolsCompute.spec.ts new file mode 100644 index 0000000..d490c63 --- /dev/null +++ b/examples/FieldHooks/ComputeFieldHooks/RemoveSymbolsCompute.spec.ts @@ -0,0 +1,49 @@ +import { TextField, Sheet, Workbook } from '@flatfile/configure' +import { SheetTester } from '../../../src/utils/testing/SheetTester' + +//This hook checks for and removes special characters in a string using RegEx. +const removeSymbolsCompute = new Sheet('removeSymbolsCompute', { + //set up your field + zipCode: TextField({ + //define a compute hook + compute: (value: any) => { + //add the regular expression you'll be using to search for invalid characters, then remove those characters + return value.replace(/[*;/{}\[\]"_#'^><|]/g, '') + }, + }), +}) + +const TestWorkbook = new Workbook({ + name: 'Test Workbook', + namespace: 'Test', + sheets: { removeSymbolsCompute }, +}) + +describe('Workbook tests ->', () => { + // here we use Sheet tester + const testSheet = new SheetTester(TestWorkbook, 'removeSymbolsCompute') + test('Remove Extra Symbols', async () => { + // for this inputRow + const inputRow = { zipCode: '234**23' } + // we expect this output row + const expectedOutputRow = { zipCode: '23423' } + const res = await testSheet.testRecord(inputRow) + //call the expectation here + expect(res).toMatchObject(expectedOutputRow) + }) + + //additional tests for additional cases + test('Test set of numbers', async () => { + const inputRow = { zipCode: '^^55555' } + const expectedOutputRow = { zipCode: '55555' } + const res = await testSheet.testRecord(inputRow) + expect(res).toMatchObject(expectedOutputRow) + }) + + test('Test trailing spaces', async () => { + const inputRow = { zipCode: 'M4B [1G5]' } + const expectedOutputRow = { zipCode: 'M4B 1G5' } + const res = await testSheet.testRecord(inputRow) + expect(res).toMatchObject(expectedOutputRow) + }) +}) From 3d5298b973862a5b9d2506c94012d0e9e2d48662 Mon Sep 17 00:00:00 2001 From: Elisa Dinsmore Date: Tue, 22 Nov 2022 15:00:11 -0500 Subject: [PATCH 08/11] reorganized and new hook example --- .../ComputeFieldHooks/dateFormat.spec.ts | 75 +++++++++++++++++++ .../removeExtraSpaces.spec.ts | 0 .../ComputeFieldHooks/removeSymbols.spec.ts} | 8 +- 3 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 examples/hooks/FieldHooks/ComputeFieldHooks/dateFormat.spec.ts rename examples/{ => hooks}/FieldHooks/ComputeFieldHooks/removeExtraSpaces.spec.ts (100%) rename examples/{FieldHooks/ComputeFieldHooks/RemoveSymbolsCompute.spec.ts => hooks/FieldHooks/ComputeFieldHooks/removeSymbols.spec.ts} (89%) diff --git a/examples/hooks/FieldHooks/ComputeFieldHooks/dateFormat.spec.ts b/examples/hooks/FieldHooks/ComputeFieldHooks/dateFormat.spec.ts new file mode 100644 index 0000000..7f53f62 --- /dev/null +++ b/examples/hooks/FieldHooks/ComputeFieldHooks/dateFormat.spec.ts @@ -0,0 +1,75 @@ +import { TextField, Sheet, Workbook } from '@flatfile/configure' +import { SheetTester } from '../../../../src/utils/testing/SheetTester' +import { format } from 'date-fns' + +//This is an example of storing the data hook outside your sheet as a helper function that can be referenced by multiple fields in your sheet. +//Here we define the function +const formatDate = + (update: string) => + (value: string): string => { + try { + return format(new Date(value), update) + } catch (err) { + return value + } + } + +//Here we call the function in the compute hook and specify the format that should be used when reformatting the dates +const testSheet = new Sheet('Test Sheet', { + joinDate: TextField({ + compute: formatDate('MM/dd/yyyy'), + }), +}) + +//Here we +const TestWorkbook = new Workbook({ + name: 'Test Workbook', + namespace: 'Test', + sheets: { testSheet }, +}) + +describe('Workbook tests ->', () => { + // here we use Sheet tester + const testSheet = new SheetTester(TestWorkbook, 'testSheet') + test('Change Date format with written month', async () => { + // for this inputRow + const inputRow = { joinDate: 'Nov 12, 2020' } + console.log(new Date(inputRow.joinDate)) + // we expect this output row + const expectedOutputRow = { joinDate: '11/12/2020' } + const res = await testSheet.testRecord(inputRow) + //call the expectation here + expect(res).toMatchObject(expectedOutputRow) + }) + + //additional tests for additional cases + test('Change Date Format with dashes', async () => { + const inputRow = { joinDate: '12-31-2020' } + console.log(new Date(inputRow.joinDate)) + const expectedOutputRow = { joinDate: '12/31/2020' } + const res = await testSheet.testRecord(inputRow) + expect(res).toMatchObject(expectedOutputRow) + }) + + test('Change date format day-month-year', async () => { + const inputRow = { joinDate: '14 Jan 2022' } + const expectedOutputRow = { joinDate: '01/14/2022' } + const res = await testSheet.testRecord(inputRow) + expect(res).toMatchObject(expectedOutputRow) + }) + + test('Change date with period delimiters', async () => { + const inputRow = { joinDate: '12.31.2022' } + const expectedOutputRow = { joinDate: '12/31/2022' } + const res = await testSheet.testRecord(inputRow) + expect(res).toMatchObject(expectedOutputRow) + }) + + //This format tool does have some limitations, so knowing what some of those are so they can be handled appropriately elsewhere can be valuable + test('Date format this hook cannot handle', async () => { + const inputRow = { joinDate: '12242022' } + const expectedOutputRow = { joinDate: '12242022' } + const res = await testSheet.testRecord(inputRow) + expect(res).toMatchObject(expectedOutputRow) + }) +}) diff --git a/examples/FieldHooks/ComputeFieldHooks/removeExtraSpaces.spec.ts b/examples/hooks/FieldHooks/ComputeFieldHooks/removeExtraSpaces.spec.ts similarity index 100% rename from examples/FieldHooks/ComputeFieldHooks/removeExtraSpaces.spec.ts rename to examples/hooks/FieldHooks/ComputeFieldHooks/removeExtraSpaces.spec.ts diff --git a/examples/FieldHooks/ComputeFieldHooks/RemoveSymbolsCompute.spec.ts b/examples/hooks/FieldHooks/ComputeFieldHooks/removeSymbols.spec.ts similarity index 89% rename from examples/FieldHooks/ComputeFieldHooks/RemoveSymbolsCompute.spec.ts rename to examples/hooks/FieldHooks/ComputeFieldHooks/removeSymbols.spec.ts index d490c63..dc507dc 100644 --- a/examples/FieldHooks/ComputeFieldHooks/RemoveSymbolsCompute.spec.ts +++ b/examples/hooks/FieldHooks/ComputeFieldHooks/removeSymbols.spec.ts @@ -1,8 +1,8 @@ import { TextField, Sheet, Workbook } from '@flatfile/configure' -import { SheetTester } from '../../../src/utils/testing/SheetTester' +import { SheetTester } from '../../../../src/utils/testing/SheetTester' //This hook checks for and removes special characters in a string using RegEx. -const removeSymbolsCompute = new Sheet('removeSymbolsCompute', { +const removeSymbolsSheet = new Sheet('removeSymbolsSheet', { //set up your field zipCode: TextField({ //define a compute hook @@ -16,12 +16,12 @@ const removeSymbolsCompute = new Sheet('removeSymbolsCompute', { const TestWorkbook = new Workbook({ name: 'Test Workbook', namespace: 'Test', - sheets: { removeSymbolsCompute }, + sheets: { removeSymbolsSheet }, }) describe('Workbook tests ->', () => { // here we use Sheet tester - const testSheet = new SheetTester(TestWorkbook, 'removeSymbolsCompute') + const testSheet = new SheetTester(TestWorkbook, 'removeSymbolsSheet') test('Remove Extra Symbols', async () => { // for this inputRow const inputRow = { zipCode: '234**23' } From 0888557ebcbc71f599bdbc7c0ca7938fd054b53e Mon Sep 17 00:00:00 2001 From: Elisa Dinsmore Date: Tue, 6 Dec 2022 15:13:11 -0500 Subject: [PATCH 09/11] adding partial validate example, missing tests --- .../formatPhoneNumber.spec.ts | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 examples/hooks/FieldHooks/ValidateFieldHooks/formatPhoneNumber.spec.ts diff --git a/examples/hooks/FieldHooks/ValidateFieldHooks/formatPhoneNumber.spec.ts b/examples/hooks/FieldHooks/ValidateFieldHooks/formatPhoneNumber.spec.ts new file mode 100644 index 0000000..d3f31ec --- /dev/null +++ b/examples/hooks/FieldHooks/ValidateFieldHooks/formatPhoneNumber.spec.ts @@ -0,0 +1,75 @@ +import { Message, TextField, Sheet, Workbook } from '@flatfile/configure' +import { SheetTester } from '../../../../src/utils/testing/SheetTester' + +const testSheet = new Sheet('Test Sheet', { + cellPhone: TextField({ + label: 'Cell Phone Number', + validate: (value: string) => { + /*the below regex matches these phone number types: + 123-456-7890 + (123) 456-7890 + 123 456 7890 + 123.456.7890 + +91 (123) 456-7890 */ + const regex = /^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/ + if (!value.match(regex)) { + return [ + new Message( + `${value} is an invalid phone number`, + 'error', + 'validate' + ), + ] + } + }, + }), +}) + +const TestWorkbook = new Workbook({ + name: 'Test Workbook', + namespace: 'Test', + sheets: { testSheet }, +}) + +describe('Workbook tests ->', () => { + // here we use Sheet tester + const testSheet = new SheetTester(TestWorkbook, 'testSheet') + test('Reformat phone number with international code', async () => { + // for this inputRow + const inputRow = { cellPhone: '15555555555' } + // we expect this output row + const expectedOutputRow = { cellPhone: '+1 (555) 555-5555' } + const res = await testSheet.testRecord(inputRow) + //call the expectation here + expect(res).toMatchObject(expectedOutputRow) + }) + + //additional tests for additional cases + test('Remove letters, number too short', async () => { + const inputRow = { cellPhone: '12321 ggg' } + const expectedOutputRow = { cellPhone: 'Invalid phone number' } + const res = await testSheet.testRecord(inputRow) + expect(res).toMatchObject(expectedOutputRow) + }) + + // test('Test trailing spaces', async () => { + // const inputRow = { cellPhone: 'asdf ' } + // const expectedOutputRow = { cellPhone: 'asdf ' } + // const res = await testSheet.testRecord(inputRow) + // expect(res).toMatchObject(expectedOutputRow) + // }) + + // test('Test multiple instances of extra spaces', async () => { + // const inputRow = { cellPhone: ' as df' } + // const expectedOutputRow = { cellPhone: ' as df' } + // const res = await testSheet.testRecord(inputRow) + // expect(res).toMatchObject(expectedOutputRow) + // }) + + // test('Test set of numbers', async () => { + // const inputRow = { cellPhone: 'Paddy Mullen' } + // const expectedOutputRow = { cellPhone: 'Paddy Mullen' } + // const res = await testSheet.testRecord(inputRow) + // expect(res).toMatchObject(expectedOutputRow) + // }) +}) From fd54de055e10bed0f698611bb616c636f568e743 Mon Sep 17 00:00:00 2001 From: Elisa Dinsmore Date: Tue, 6 Dec 2022 15:48:05 -0500 Subject: [PATCH 10/11] add record hook example --- .../conditionallyEmptyField.spec.ts | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 examples/hooks/RecordHooks/conditionallyEmptyField.spec.ts diff --git a/examples/hooks/RecordHooks/conditionallyEmptyField.spec.ts b/examples/hooks/RecordHooks/conditionallyEmptyField.spec.ts new file mode 100644 index 0000000..5ed100e --- /dev/null +++ b/examples/hooks/RecordHooks/conditionallyEmptyField.spec.ts @@ -0,0 +1,51 @@ +import { NumberField, TextField, Sheet, Workbook } from '@flatfile/configure' +import { SheetTester } from '../../../src/utils/testing/SheetTester' + +//this is a basic example of how to modify a field based on other fields +const testSheet = new Sheet( + 'Test Sheet', + { + paymentStatus: TextField(), + amount: NumberField(), + }, + { + //because this hook is comparing multiple fields and both modifying a field and adding a message, it must be run as a recordHook rather than a fieldHook + recordCompute: (record, session, logger) => { + if (record.get('paymentStatus') && !record.get('amount')) { + record.set('paymentStatus', '') + record.addWarning( + 'paymentStatus', + 'Amount must be greater than 0 to select a Payment Status' + ) + } + }, + } +) + +const TestWorkbook = new Workbook({ + name: 'Test Workbook', + namespace: 'Test', + sheets: { testSheet }, +}) + +describe('Workbook tests ->', () => { + // here we use Sheet tester + const testSheet = new SheetTester(TestWorkbook, 'testSheet') + test('Empty the Cell', async () => { + // for this inputRow + const inputRow = { paymentStatus: 'Paid', amount: null } + // we expect this output row + const expectedOutputRow = { paymentStatus: '', amount: null } + const res = await testSheet.testRecord(inputRow) + //call the expectation here + expect(res).toMatchObject(expectedOutputRow) + }) + + //additional tests for additional cases + test('test value in paymentStatus', async () => { + const inputRow = { paymentStatus: 'Paid', amount: 2500 } + const expectedOutputRow = { paymentStatus: 'Paid', amount: 2500 } + const res = await testSheet.testRecord(inputRow) + expect(res).toMatchObject(expectedOutputRow) + }) +}) From 957fb1b6dda96e66cf17d11a26a1bfe7da68a752 Mon Sep 17 00:00:00 2001 From: Elisa Dinsmore Date: Mon, 19 Dec 2022 13:35:35 -0500 Subject: [PATCH 11/11] Conditionally Modify Field Example --- .../conditionallyModifyField.spec.ts | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 examples/hooks/RecordHooks/conditionallyModifyField.spec.ts diff --git a/examples/hooks/RecordHooks/conditionallyModifyField.spec.ts b/examples/hooks/RecordHooks/conditionallyModifyField.spec.ts new file mode 100644 index 0000000..481a40a --- /dev/null +++ b/examples/hooks/RecordHooks/conditionallyModifyField.spec.ts @@ -0,0 +1,76 @@ +import { Sheet, Workbook, BooleanField, TextField } from '@flatfile/configure' +import { SheetTester } from '../../../src/utils/testing/SheetTester' + +//this Data Hook can be used to update the contents of a cell based on whether or not another cell passes RegEx validation. +//This method of setting fields at validations at the top of a sheet allows you to set all RegEx and messages in one easily referencable place. +const FIELDS = { + accountId: { + regex: /^.{1,20}$/, + message: 'Account Number must be 20 characters or less', + }, +} + +const testSheet = new Sheet( + 'Test Sheet', + { + accountId: TextField(), + accountStatus: BooleanField(), + }, + { + recordCompute: (record) => { + //ensuring the accountId is returned as a string + const accountNum = record.get('accountId') as string + //here we reference the fields we defined at the top, the RegEx, and the error message we want to display if the field does not pass its RegEx. + if (FIELDS['accountId'].regex.test(accountNum) === false) { + record.set('accountStatus', false) + record.addError('accountId', FIELDS.accountId.message) + } else { + record.set('accountStatus', true) + } + }, + } +) + +const TestWorkbook = new Workbook({ + name: 'Test Workbook', + namespace: 'Test', + sheets: { testSheet }, +}) + +describe('Workbook tests ->', () => { + // here we use Sheet tester + const testSheet = new SheetTester(TestWorkbook, 'testSheet') + test('21 Characters', async () => { + // for this inputRow + const inputRow = { accountId: '1234567890asdfghjkjhg', accountStatus: true } + // we expect this output row + const expectedOutputRow = { + accountId: '1234567890asdfghjkjhg', + accountStatus: false, + } + const res = await testSheet.testRecord(inputRow) + //call the expectation here + expect(res).toMatchObject(expectedOutputRow) + }) + + //additional tests for additional cases + test('19 Characters', async () => { + const inputRow = { accountId: '1234567890asdfghjkj', accountStatus: true } + const expectedOutputRow = { + accountId: '1234567890asdfghjkj', + accountStatus: true, + } + const res = await testSheet.testRecord(inputRow) + expect(res).toMatchObject(expectedOutputRow) + }) + + test('Test Unexpected Characters', async () => { + const inputRow = { accountId: 'd sdfl &&^$% 23', accountStatus: true } + const expectedOutputRow = { + accountId: 'd sdfl &&^$% 23', + accountStatus: true, + } + const res = await testSheet.testRecord(inputRow) + expect(res).toMatchObject(expectedOutputRow) + }) +})