sadpandajoe commented on code in PR #45591: URL: https://github.com/apache/superset/pull/45591#discussion_r4366220312
########## superset-frontend/src/features/groups/utils.test.ts: ########## @@ -0,0 +1,228 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { SupersetClient } from '@superset-ui/core'; +import rison from 'rison'; +import { render, screen } from 'spec/helpers/testing-library'; +import { + fetchUserOptions, + toUserSelectOption, + USER_OPTION_FILTER_PROPS, + userOptionSortComparator, +} from './utils'; + +type GetResponse = Awaited<ReturnType<typeof SupersetClient.get>>; + +const getMock = jest.spyOn(SupersetClient, 'get'); + +const mockUsersResponse = ( + result: Record<string, unknown>[], + count = result.length, +) => + getMock.mockResolvedValue({ + json: { count, result }, + } as unknown as GetResponse); + +const decodeQuery = () => { + const { endpoint } = getMock.mock.calls[0][0] as { endpoint: string }; + const q = new URLSearchParams(endpoint.split('?')[1]).get('q') ?? ''; + return rison.decode(q) as Record<string, unknown>; +}; + +afterEach(() => { + getMock.mockReset(); +}); + +test('fetchUserOptions sends the search term as a server-side filter', async () => { + mockUsersResponse([ + { + id: 7, + username: 'jsmith', + first_name: 'Jane', + last_name: 'Smith', + email: '[email protected]', + }, + ]); + + const result = await fetchUserOptions('Jane Sm', 0, 100, jest.fn()); + + expect(decodeQuery()).toEqual({ + filters: [{ col: 'username', opr: 'user_all_text', value: 'Jane Sm' }], + page: 0, + page_size: 100, + order_column: 'username', + order_direction: 'asc', + }); + expect(result).toEqual({ + data: [ + { + value: 7, + label: expect.anything(), + title: 'Jane Smith', + textLabel: 'Jane Smith', + subjectDetail: '[email protected]', + username: 'jsmith', + }, + ], + totalCount: 1, + }); +}); + +test('fetchUserOptions trims the search term before sending it', async () => { + mockUsersResponse([]); + + await fetchUserOptions(' jane ', 0, 100, jest.fn()); + + expect(decodeQuery()).toEqual({ + filters: [{ col: 'username', opr: 'user_all_text', value: 'jane' }], + page: 0, + page_size: 100, + order_column: 'username', + order_direction: 'asc', + }); +}); + +test.each([ + ['an empty', ''], + ['a whitespace-only', ' '], +])( + 'fetchUserOptions sends no filters for %s search term', + async (_label, filterValue) => { + mockUsersResponse([{ id: 1, username: 'admin' }], 250); + + const result = await fetchUserOptions(filterValue, 2, 100, jest.fn()); + + expect(decodeQuery()).toEqual({ + filters: [], + page: 2, + page_size: 100, + order_column: 'username', + order_direction: 'asc', + }); + expect(result).toEqual({ + data: [ + expect.objectContaining({ + value: 1, + title: 'admin', + username: 'admin', + }), + ], + totalCount: 250, + }); + }, +); + +test('fetchUserOptions shows a toast and returns no options on error', async () => { + getMock.mockRejectedValue(new Error('boom')); + const addDangerToast = jest.fn(); + + const result = await fetchUserOptions('jane', 0, 100, addDangerToast); + + expect(addDangerToast).toHaveBeenCalledWith( + 'There was an error while fetching users', + ); + expect(result).toEqual({ data: [], totalCount: 0 }); +}); + +test('toUserSelectOption shows the email under the display name', () => { + const option = toUserSelectOption({ + id: 7, + username: 'jsmith', + first_name: 'Jane', + last_name: 'Smith', + email: '[email protected]', + }); + + render(option.label); + + expect(screen.getByText('Jane Smith')).toBeInTheDocument(); + expect(screen.getByText('[email protected]')).toBeInTheDocument(); + expect(option).toMatchObject({ + value: 7, + title: 'Jane Smith', + textLabel: 'Jane Smith', + subjectDetail: '[email protected]', + username: 'jsmith', + }); +}); + +test('USER_OPTION_FILTER_PROPS covers every column the server search matches', () => { + // The server-side user_all_text filter matches name, email and username. + // Each must be a plain-text prop on the option, or AsyncSelect's client-side + // re-filter hides a user the server returned. + const option = toUserSelectOption({ + id: 7, + username: 'jsmith', + first_name: 'Jane', + last_name: 'Smith', + email: '[email protected]', + }); + + expect(USER_OPTION_FILTER_PROPS).toEqual([ + 'textLabel', + 'subjectDetail', + 'username', + ]); + expect( + USER_OPTION_FILTER_PROPS.map(prop => option[prop as keyof typeof option]), + ).toEqual(['Jane Smith', '[email protected]', 'jsmith']); +}); + +test('toUserSelectOption falls back to the username and omits a missing email', () => { + const option = toUserSelectOption({ id: 3, username: 'svc_reporting' }); + + render(option.label); + + expect(screen.getByText('svc_reporting')).toBeInTheDocument(); + expect(option).toMatchObject({ + value: 3, + title: 'svc_reporting', + textLabel: 'svc_reporting', + subjectDetail: undefined, + }); +}); + +test('userOptionSortComparator orders users by display name, not id', () => { Review Comment: This only exercises `userOptionSortComparator` in isolation; the Role and Group picker tests each return a single matching user. If `sortComparator={userOptionSortComparator}` is dropped from either `AsyncSelect`, every test here still passes while the dropdown goes back to ordering users by numeric id (Zoe, Amy, Max for ids 1, 2, 3). Could a picker-level test return those three users as React-label options and assert the rendered order is Amy, Max, Zoe for both the Roles and Groups modals? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
