Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Add unit tests for grid utils format sort/filter url params

Joshen Lim060c71d2b5de
diff --git a/studio/components/grid/SupabaseGrid.utils.ts b/studio/components/grid/SupabaseGrid.utils.ts
index b0167be93aa..49a133d2043 100644
--- a/studio/components/grid/SupabaseGrid.utils.ts
+++ b/studio/components/grid/SupabaseGrid.utils.ts
@@ -32,7 +32,6 @@ export function cleanupProps(props: SupabaseGridProps) {
}
}
-// [JOSHEN TODO] Write tests for this
export function formatSortURLParams(sort?: string[]) {
return (
Array.isArray(sort)
@@ -48,7 +47,6 @@ export function formatSortURLParams(sort?: string[]) {
) as Sort[]
}
-// [JOSHEN TODO] Write tests for this
export function formatFilterURLParams(filter?: string[]): Filter[] {
return (
Array.isArray(filter)
diff --git a/studio/components/grid/utils/filter.ts b/studio/components/grid/utils/filter.ts
deleted file mode 100644
index daa8a8b2ed8..00000000000
--- a/studio/components/grid/utils/filter.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-import { uuidv4 } from 'lib/helpers'
-import { Filter, SavedState, SupaTable } from '../types'
-
-// [JOSHEN TODO] Deprecated but double check
-export function getInitialFilters(table: SupaTable, savedState?: SavedState): Filter[] {
- if (savedState?.filters) {
- // verify column still exists
- const filters = savedState.filters.filter((x) => {
- const found = table.columns.find((y) => y.name === x.column)
- return found ? true : false
- })
- if (filters?.length > 0) {
- return filters.map((filter) => ({ ...filter, id: filter?.id || uuidv4() }))
- }
- }
- return []
-}
diff --git a/studio/components/grid/utils/index.ts b/studio/components/grid/utils/index.ts
index 93cc0994931..156acf5d607 100644
--- a/studio/components/grid/utils/index.ts
+++ b/studio/components/grid/utils/index.ts
@@ -1,7 +1,5 @@
-export * from './common';
-export * from './column';
-export * from './hooks';
-export * from './filter';
-export * from './row';
-export * from './sort';
-export * from './types';
+export * from './common'
+export * from './column'
+export * from './hooks'
+export * from './row'
+export * from './types'
diff --git a/studio/components/grid/utils/sort.ts b/studio/components/grid/utils/sort.ts
deleted file mode 100644
index a723255c01c..00000000000
--- a/studio/components/grid/utils/sort.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-import { SavedState, Sort, SupaTable } from '../types';
-
-export function getInitialSorts(
- table: SupaTable,
- savedState?: SavedState
-): Sort[] {
- if (savedState?.sorts) {
- // verify column still exists
- const sorts = savedState.sorts.filter((x) => {
- const found = table.columns.find((y) => y.name === x.column);
- return found ? true : false;
- });
- if (sorts?.length > 0) return sorts;
- }
-
- // default sorts
- if (!table?.columns || table?.columns?.length <= 0) return [];
- const firstColumn = table?.columns[0];
- return [{ column: firstColumn.name, ascending: true }];
-}
diff --git a/studio/tests/components/Grid/Grid.utils.test.js b/studio/tests/components/Grid/Grid.utils.test.js
new file mode 100644
index 00000000000..c727de07269
--- /dev/null
+++ b/studio/tests/components/Grid/Grid.utils.test.js
@@ -0,0 +1,64 @@
+import { formatSortURLParams, formatFilterURLParams } from 'components/grid/SupabaseGrid.utils'
+
+// Sort URL syntax: `column:order`
+describe('SupabaseGrid.utils: formatSortURLParams', () => {
+ test('should return an array of sort options based on URL params', () => {
+ const mockInput = ['id:asc', 'name:desc']
+ const output = formatSortURLParams(mockInput)
+ expect(output).toStrictEqual([
+ {
+ column: 'id',
+ ascending: true,
+ },
+ {
+ column: 'name',
+ ascending: false,
+ },
+ ])
+ })
+ test('should reject any malformed sort options based on URL params', () => {
+ const mockInput = ['id', 'name:asc', ':asc']
+ const output = formatSortURLParams(mockInput)
+ expect(output).toStrictEqual([
+ {
+ column: 'name',
+ ascending: true,
+ },
+ ])
+ })
+})
+
+// Filter URL syntax: `column:operatorAbbreviation:value`
+describe('SupabaseGrid.utils: formatFilterURLParams', () => {
+ test('should return an array of filter options based on URL params', () => {
+ const mockInput = ['id:gte:20', 'id:lte:40']
+ const output = formatFilterURLParams(mockInput)
+ expect(output).toHaveLength(2)
+ expect(output[0]).toHaveProperty('id')
+ expect(output[0]).toHaveProperty('column', 'id')
+ expect(output[0]).toHaveProperty('operator', '>=')
+ expect(output[0]).toHaveProperty('value', '20')
+ expect(output[1]).toHaveProperty('id')
+ expect(output[1]).toHaveProperty('column', 'id')
+ expect(output[1]).toHaveProperty('operator', '<=')
+ expect(output[1]).toHaveProperty('value', '40')
+ })
+ test('should reject any malformed filter options based on URL params', () => {
+ const mockInput = ['id', ':gte', ':50', 'id:eq:10']
+ const output = formatFilterURLParams(mockInput)
+ expect(output).toHaveLength(1)
+ })
+ test('should reject any filter options with unrecognized operator', () => {
+ const mockInput = ['id:meme:40', 'name:eq:town']
+ const output = formatFilterURLParams(mockInput)
+ expect(output).toHaveLength(1)
+ })
+ test('should allow filter options to have empty value based on URL params', () => {
+ const mockInput = ['id:ilike:']
+ const output = formatFilterURLParams(mockInput)
+ expect(output).toHaveLength(1)
+ expect(output[0]).toHaveProperty('column', 'id')
+ expect(output[0]).toHaveProperty('operator', '~~*')
+ expect(output[0]).toHaveProperty('value', '')
+ })
+})