From 9f161d886b92a483a2425aeb1c82340efef8b603 Mon Sep 17 00:00:00 2001 From: Ville Brofeldt Date: Sun, 18 Dec 2022 10:23:57 +0200 Subject: [PATCH] add tests --- .../src/views/CRUD/utils.test.tsx | 166 ++++++++++++++++++ superset-frontend/src/views/CRUD/utils.tsx | 35 ++-- .../src/views/CRUD/welcome/SavedQueries.tsx | 9 - 3 files changed, 179 insertions(+), 31 deletions(-) diff --git a/superset-frontend/src/views/CRUD/utils.test.tsx b/superset-frontend/src/views/CRUD/utils.test.tsx index af89c94413e22..fa41455d8508b 100644 --- a/superset-frontend/src/views/CRUD/utils.test.tsx +++ b/superset-frontend/src/views/CRUD/utils.test.tsx @@ -20,11 +20,15 @@ import rison from 'rison'; import { checkUploadExtensions, getAlreadyExists, + getFilterValues, getPasswordsNeeded, hasTerminalValidation, isAlreadyExists, isNeedsPassword, } from 'src/views/CRUD/utils'; +import { User } from 'src/types/bootstrapTypes'; +import { Filter, TableTab } from './types'; +import { WelcomeTable } from './welcome/types'; const terminalErrors = { errors: [ @@ -228,3 +232,165 @@ test('checkUploadExtensions should return valid upload extensions', () => { checkUploadExtensions(randomExtensionThree, uploadExtensionTest), ).toBeFalsy(); }); + +test('getFilterValues', () => { + const userId = 1234; + const mockUser: User = { + firstName: 'foo', + lastName: 'bar', + username: 'baz', + userId, + isActive: true, + isAnonymous: false, + }; + const testCases: [ + TableTab, + WelcomeTable, + User | undefined, + Filter[] | undefined, + ReturnType, + ][] = [ + [ + TableTab.Mine, + WelcomeTable.SavedQueries, + mockUser, + undefined, + [ + { + id: 'created_by', + operator: 'rel_o_m', + value: `${userId}`, + }, + ], + ], + [ + TableTab.Favorite, + WelcomeTable.SavedQueries, + mockUser, + undefined, + [ + { + id: 'id', + operator: 'saved_query_is_fav', + value: true, + }, + ], + ], + [ + TableTab.Created, + WelcomeTable.Charts, + mockUser, + undefined, + [ + { + id: 'created_by', + operator: 'rel_o_m', + value: `${userId}`, + }, + ], + ], + [ + TableTab.Created, + WelcomeTable.Dashboards, + mockUser, + undefined, + [ + { + id: 'created_by', + operator: 'rel_o_m', + value: `${userId}`, + }, + ], + ], + [ + TableTab.Created, + WelcomeTable.Recents, + mockUser, + undefined, + [ + { + id: 'created_by', + operator: 'rel_o_m', + value: `${userId}`, + }, + ], + ], + [ + TableTab.Mine, + WelcomeTable.Charts, + mockUser, + undefined, + [ + { + id: 'owners', + operator: 'rel_m_m', + value: `${userId}`, + }, + ], + ], + [ + TableTab.Mine, + WelcomeTable.Dashboards, + mockUser, + undefined, + [ + { + id: 'owners', + operator: 'rel_m_m', + value: `${userId}`, + }, + ], + ], + [ + TableTab.Favorite, + WelcomeTable.Dashboards, + mockUser, + undefined, + [ + { + id: 'id', + operator: 'dashboard_is_favorite', + value: true, + }, + ], + ], + [ + TableTab.Favorite, + WelcomeTable.Charts, + mockUser, + undefined, + [ + { + id: 'id', + operator: 'chart_is_favorite', + value: true, + }, + ], + ], + [ + TableTab.Other, + WelcomeTable.Charts, + mockUser, + [ + { + col: 'created_by', + opr: 'rel_o_m', + value: 0, + }, + ], + [ + { + id: 'created_by', + operator: 'rel_o_m', + value: 0, + }, + ], + ], + ]; + testCases.forEach(testCase => { + const [tab, welcomeTable, user, otherTabFilters, expectedValue] = testCase; + expect(getFilterValues(tab, welcomeTable, user, otherTabFilters)).toEqual( + expectedValue, + ); + }); +}); diff --git a/superset-frontend/src/views/CRUD/utils.tsx b/superset-frontend/src/views/CRUD/utils.tsx index 4e2af4feeee7e..bbb985609d4b3 100644 --- a/superset-frontend/src/views/CRUD/utils.tsx +++ b/superset-frontend/src/views/CRUD/utils.tsx @@ -445,7 +445,10 @@ export function getFilterValues( user?: User, otherTabFilters?: Filter[], ): FilterValue[] { - if (welcomeTable === WelcomeTable.SavedQueries && tab === TableTab.Mine) { + if ( + tab === TableTab.Created || + (welcomeTable === WelcomeTable.SavedQueries && tab === TableTab.Mine) + ) { return [ { id: 'created_by', @@ -454,7 +457,7 @@ export function getFilterValues( }, ]; } - if (welcomeTable === WelcomeTable.SavedQueries) { + if (welcomeTable === WelcomeTable.SavedQueries && tab === TableTab.Favorite) { return [ { id: 'id', @@ -463,15 +466,6 @@ export function getFilterValues( }, ]; } - if (tab === TableTab.Created && user) { - return [ - { - id: 'created_by', - operator: 'rel_o_m', - value: `${user.userId}`, - }, - ]; - } if (tab === TableTab.Mine && user) { return [ { @@ -481,20 +475,17 @@ export function getFilterValues( }, ]; } - if (welcomeTable === WelcomeTable.Dashboards && tab === TableTab.Favorite) { - return [ - { - id: 'id', - operator: 'dashboard_is_favorite', - value: true, - }, - ]; - } - if (welcomeTable === WelcomeTable.Charts && tab === TableTab.Favorite) { + if ( + tab === TableTab.Favorite && + [WelcomeTable.Dashboards, WelcomeTable.Charts].includes(welcomeTable) + ) { return [ { id: 'id', - operator: 'chart_is_favorite', + operator: + welcomeTable === WelcomeTable.Dashboards + ? 'dashboard_is_favorite' + : 'chart_is_favorite', value: true, }, ]; diff --git a/superset-frontend/src/views/CRUD/welcome/SavedQueries.tsx b/superset-frontend/src/views/CRUD/welcome/SavedQueries.tsx index 3e7c424239c71..4e323e633f225 100644 --- a/superset-frontend/src/views/CRUD/welcome/SavedQueries.tsx +++ b/superset-frontend/src/views/CRUD/welcome/SavedQueries.tsx @@ -246,15 +246,6 @@ const SavedQueries = ({ { - getData('Favorite').then(() => setQueryFilter('Favorite')); - }, - }, - */ { name: TableTab.Mine, label: t('Mine'),