Files
supabase/apps/studio/tests/components/Editor/RowEditor.utils.test.ts
Jordi Enric 764d10986d vitest & msw integration (#26303)
* Fix tests in tests/unit, tests/components and files under tests, looking into tests/pages

* Fix tests under pages/projects root

* Fix

* Comment out broken tests that im stuck with

* Fix api-report.test

* Fix storage-report-test

* chore: fix some tests

* chore: remove logging

* Fix LogsPreviewer.test.js

* Fix most of logs-query-test

* Skip broken tests instead of false positiving them

* Replace jest with vitest

* Rename all *.test.js to *.test.ts

* Configure vitest to work with jsx

* fix vitest issues, fix tests, skip broken tests, add msw, add next-router-mock

* uncomment file

* add tests for msw and nrm

* Fix failing tests

* fix tests in RowEditor

* fix datepicker tests

* fix type errors and comment out tests that need some refactoring

* leave 1 test so test script works

* rm clog and aaaaa

* rename script

* move msw to studio

* add pckg json which i forgot in last commit

* rm consolelog

* move vitest ui dep

* Move next-router-mock to studio.

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
Co-authored-by: TzeYiing <ty@tzeyiing.com>
Co-authored-by: Kamil Ogórek <kamil.ogorek@gmail.com>
Co-authored-by: Terry Sutton <saltcod@gmail.com>
Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
2024-05-29 17:31:20 +02:00

236 lines
6.5 KiB
TypeScript

import { vi } from 'vitest'
import {
generateRowObjectFromFields,
parseValue,
} from 'components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils'
import { RowField } from 'components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.types'
describe('parseValue', () => {
it('should return null when originalValue is null', () => {
const originalValue = null
const format = 'some format'
expect(parseValue(originalValue, format)).toBeNull()
})
it('should return originalValue when it is 0', () => {
const originalValue = 0
const format = 'some format'
expect(parseValue(originalValue, format)).toEqual(originalValue)
})
it('should return originalValue when it is an empty string', () => {
const originalValue = ''
const format = 'some format'
expect(parseValue(originalValue, format)).toEqual(originalValue)
})
it('should return originalValue when it is a number and format is not provided', () => {
const originalValue = 42
const format = ''
expect(parseValue(originalValue, format)).toEqual(originalValue)
})
it('should return JSON string representation when originalValue is an empty array', () => {
const originalValue: any[] = []
const format = 'some format'
const expectedValue = JSON.stringify(originalValue)
expect(parseValue(originalValue, format)).toEqual(expectedValue)
})
it('should return JSON string representation when originalValue is an empty object', () => {
const originalValue = {}
const format = 'some format'
const expectedValue = JSON.stringify(originalValue)
expect(parseValue(originalValue, format)).toEqual(expectedValue)
})
it('should return JSON string representation when originalValue is an object', () => {
const originalValue = { key: 'value' }
const format = 'some format'
expect(parseValue(originalValue, format)).toEqual(JSON.stringify(originalValue))
})
it('should handle complex nested object with titles correctly', () => {
const originalValue = {
glossary: {
title: 'parent title',
subItem: {
title: 'subItem title',
items: ['item1', 'item2'],
},
},
}
const format = 'some format'
const expectedValue = JSON.stringify(originalValue)
expect(parseValue(originalValue, format)).toEqual(expectedValue)
})
it('should handle object with all values set to 0 correctly', () => {
const originalValue = {
width: 0,
height: 0,
length: 0,
weight: 0,
}
const format = 'some format'
const expectedValue = JSON.stringify(originalValue)
expect(parseValue(originalValue, format)).toEqual(expectedValue)
})
it('should return string representation of originalValue when it is a boolean', () => {
const originalValue = true
const format = 'some format'
expect(parseValue(originalValue, format)).toEqual(originalValue.toString())
})
it('should return originalValue for other cases', () => {
const originalValue = 'some value'
const format = 'some format'
expect(parseValue(originalValue, format)).toEqual(originalValue)
})
it('should return originalValue even when an error occurs', () => {
const originalValue = 'some value'
const format = 'some format'
// Mocking an error occurring during parsing
JSON.stringify = vi.fn(() => {
throw new Error('Mocked error')
})
expect(parseValue(originalValue, format)).toEqual(originalValue)
})
})
describe('generateRowObjectFromFields', () => {
it('should not force NULL values', () => {
const sampleRowFields: RowField[] = [
{
id: '1',
name: 'id',
value: '',
comment: '',
defaultValue: null,
format: 'int8',
enums: [],
isNullable: false,
isIdentity: false,
isPrimaryKey: false,
},
{
id: '2',
name: 'time_not_null',
value: '',
comment: '',
defaultValue: 'now()',
format: 'timestamptz',
isNullable: false,
enums: [],
isIdentity: false,
isPrimaryKey: false,
},
{
id: '3',
name: 'time_nullable',
value: '',
comment: '',
defaultValue: 'now()',
format: 'timestamptz',
isNullable: true,
enums: [],
isIdentity: false,
isPrimaryKey: false,
},
]
const result = generateRowObjectFromFields(sampleRowFields)
expect(result).toEqual({})
})
it('should discern EMPTY values for text', () => {
const sampleRowFields: RowField[] = [
{
id: '1',
name: 'id',
value: '',
comment: '',
defaultValue: null,
format: 'int8',
enums: [],
isNullable: false,
isIdentity: false,
isPrimaryKey: false,
},
{
id: '2',
name: 'name',
value: '',
comment: '',
defaultValue: null,
format: 'text',
enums: [],
isNullable: false,
isIdentity: false,
isPrimaryKey: false,
},
]
const result = generateRowObjectFromFields(sampleRowFields)
expect(result).toEqual({ name: '' })
})
it('should discern NULL values for text', () => {
const sampleRowFields: RowField[] = [
{
id: '1',
name: 'id',
value: '',
comment: '',
defaultValue: null,
format: 'int8',
enums: [],
isNullable: false,
isIdentity: false,
isPrimaryKey: false,
},
{
id: '2',
name: 'name',
value: null,
comment: '',
defaultValue: null,
format: 'text',
enums: [],
isNullable: false,
isIdentity: false,
isPrimaryKey: false,
},
]
const result = generateRowObjectFromFields(sampleRowFields)
expect(result).toEqual({})
})
it('should discern NULL values for booleans', () => {
const sampleRowFields: RowField[] = [
{
id: '1',
name: 'id',
value: '',
comment: '',
defaultValue: null,
format: 'int8',
enums: [],
isNullable: false,
isIdentity: false,
isPrimaryKey: false,
},
{
id: '2',
name: 'bool-test',
value: null,
comment: '',
defaultValue: null,
format: 'bool',
enums: [],
isNullable: false,
isIdentity: false,
isPrimaryKey: false,
},
]
const result = generateRowObjectFromFields(sampleRowFields)
expect(result).toEqual({})
})
})