Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
villebro committed Dec 18, 2022
1 parent d0fcc95 commit 9f161d8
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 31 deletions.
166 changes: 166 additions & 0 deletions superset-frontend/src/views/CRUD/utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -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<typeof getFilterValues>,
][] = [
[
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,
);
});
});
35 changes: 13 additions & 22 deletions superset-frontend/src/views/CRUD/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -454,7 +457,7 @@ export function getFilterValues(
},
];
}
if (welcomeTable === WelcomeTable.SavedQueries) {
if (welcomeTable === WelcomeTable.SavedQueries && tab === TableTab.Favorite) {
return [
{
id: 'id',
Expand All @@ -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 [
{
Expand All @@ -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,
},
];
Expand Down
9 changes: 0 additions & 9 deletions superset-frontend/src/views/CRUD/welcome/SavedQueries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,6 @@ const SavedQueries = ({
<SubMenu
activeChild={activeTab}
tabs={[
/* @TODO uncomment when fav functionality is implemented
{
name: 'Favorite',
label: t('Favorite'),
onClick: () => {
getData('Favorite').then(() => setQueryFilter('Favorite'));
},
},
*/
{
name: TableTab.Mine,
label: t('Mine'),
Expand Down

0 comments on commit 9f161d8

Please sign in to comment.