From 73da9a767103227f56d32611b0a341a5bdb84a6e Mon Sep 17 00:00:00 2001 From: Muhammed Mustafa Date: Fri, 2 Sep 2022 11:53:53 +0530 Subject: [PATCH] refactor/Added unit test for update and delete table row api --- .../__tests__/unit/rest/tests/factory/row.ts | 2 + .../unit/rest/tests/tableRow.test.ts | 76 ++++++++++++++++++- 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/packages/nocodb/src/__tests__/unit/rest/tests/factory/row.ts b/packages/nocodb/src/__tests__/unit/rest/tests/factory/row.ts index ca7de6efc6..38ebe371c5 100644 --- a/packages/nocodb/src/__tests__/unit/rest/tests/factory/row.ts +++ b/packages/nocodb/src/__tests__/unit/rest/tests/factory/row.ts @@ -13,6 +13,8 @@ const rowValue = (column: ColumnType, index: number) => { return '2020-01-01'; case UITypes.DateTime: return '2020-01-01 00:00:00'; + case UITypes.Email: + return `test-${index}@example.com`; default: return `test-${index}`; } diff --git a/packages/nocodb/src/__tests__/unit/rest/tests/tableRow.test.ts b/packages/nocodb/src/__tests__/unit/rest/tests/tableRow.test.ts index 90b8b85b2c..42de8f2c64 100644 --- a/packages/nocodb/src/__tests__/unit/rest/tests/tableRow.test.ts +++ b/packages/nocodb/src/__tests__/unit/rest/tests/tableRow.test.ts @@ -10,7 +10,7 @@ import { createRollupColumn, } from './factory/column'; import { createTable } from './factory/table'; -import { createRow } from './factory/row'; +import { createRow, getRow } from './factory/row'; const isColumnsCorrectInResponse = (row, columns: ColumnType[]) => { const responseColumnsListStr = Object.keys(row).sort().join(','); @@ -1102,6 +1102,80 @@ function tableTest() { throw new Error('Wrong update'); } }); + + it('Update table row with validation and invalid data', async function () { + const table = await createTable(context, project); + const emailColumn = await createColumn(context, table, { + title: 'Email', + column_name: 'email', + uidt: UITypes.Email, + meta: { + validate: true, + }, + }); + const columns = await table.getColumns(); + const row = await createRow(context, project, table, columns, 0); + + await request(context.app) + .patch(`/api/v1/db/data/noco/${project.id}/${table.id}/${row['Id']}`) + .set('xc-auth', context.token) + .send({ + [emailColumn.column_name]: 'invalidemail', + }) + .expect(400); + }); + + // todo: Test webhooks of before and after update + // todo: Test with form view + + it('Update table row with validation and valid data', async function () { + const table = await createTable(context, project); + const emailColumn = await createColumn(context, table, { + title: 'Email', + column_name: 'email', + uidt: UITypes.Email, + meta: { + validate: true, + }, + }); + const columns = await table.getColumns(); + const row = await createRow(context, project, table, columns, 0); + + const response = await request(context.app) + .patch(`/api/v1/db/data/noco/${project.id}/${table.id}/${row['Id']}`) + .set('xc-auth', context.token) + .send({ + [emailColumn.column_name]: 'valid@example.com', + }) + .expect(200); + + const updatedRow = await getRow( + context, + project, + table, + response.body['Id'] + ); + if (updatedRow[emailColumn.title] !== 'valid@example.com') { + throw new Error('Wrong update'); + } + }); + + it('Delete table row', async function () { + const table = await createTable(context, project); + const columns = await table.getColumns(); + const row = await createRow(context, project, table, columns, 0); + + await request(context.app) + .delete(`/api/v1/db/data/noco/${project.id}/${table.id}/${row['Id']}`) + .set('xc-auth', context.token) + .expect(200); + + const deleteRow = await getRow(context, project, table, row['Id']); + if (deleteRow && Object.keys(deleteRow).length > 0) { + console.log(deleteRow); + throw new Error('Wrong delete'); + } + }); } export default function () {