kgabryje commented on code in PR #32207: URL: https://github.com/apache/superset/pull/32207#discussion_r1951101168
########## superset-frontend/src/dashboard/components/SliceAdder.test.tsx: ########## @@ -16,181 +16,239 @@ * specific language governing permissions and limitations * under the License. */ -import { shallow, ShallowWrapper } from 'enzyme'; -import sinon from 'sinon'; - -import SliceAdder, { - ChartList, - DEFAULT_SORT_KEY, - SliceAdderProps, -} from 'src/dashboard/components/SliceAdder'; +import { + fireEvent, + render, + screen, + userEvent, +} from 'spec/helpers/testing-library'; +import { Provider } from 'react-redux'; +import { + ThemeProvider, + supersetTheme, + DatasourceType, +} from '@superset-ui/core'; import { sliceEntitiesForDashboard as mockSliceEntities } from 'spec/fixtures/mockSliceEntities'; -import { styledShallow } from 'spec/helpers/theming'; +import { configureStore } from '@reduxjs/toolkit'; +import SliceAdder, { SliceAdderProps } from './SliceAdder'; + +// Mock the Select component to avoid debounce issues +jest.mock('@superset-ui/core', () => ({ + ...jest.requireActual('@superset-ui/core'), + Select: ({ value, onChange, options }: any) => ( + <select + data-test="select" + value={value} + onChange={e => onChange(e.target.value)} + > + {options?.map((opt: any) => ( + <option key={opt.value} value={opt.value}> + {opt.label} + </option> + ))} + </select> + ), +})); + +jest.mock('lodash/debounce', () => { + const debounced = (fn: Function) => { + const debouncedFn = ((...args: any[]) => + fn(...args)) as unknown as Function & { + cancel: () => void; + }; + debouncedFn.cancel = () => {}; + return debouncedFn; + }; + return debounced; +}); -jest.mock( - 'lodash/debounce', - () => (fn: { throttle: jest.Mock<any, any, any> }) => { - // eslint-disable-next-line no-param-reassign - fn.throttle = jest.fn(); - return fn; - }, -); +const mockStore = configureStore({ + reducer: (state = { common: { locale: 'en' } }) => state, +}); -describe('SliceAdder', () => { - const props: SliceAdderProps = { - slices: { - ...mockSliceEntities.slices, - }, - fetchSlices: jest.fn(), - updateSlices: jest.fn(), - selectedSliceIds: [127, 128], - userId: 1, - dashboardId: 0, - editMode: false, - errorMessage: '', - isLoading: false, - lastUpdated: 0, - }; - const errorProps = { - ...props, - errorMessage: 'this is error', - }; - describe('SliceAdder.sortByComparator', () => { - it('should sort by timestamp descending', () => { - const sortedTimestamps = Object.values(props.slices) - .sort(SliceAdder.sortByComparator('changed_on')) - .map(slice => slice.changed_on); - expect( - sortedTimestamps.every((currentTimestamp, index) => { - if (index === 0) { - return true; - } - return currentTimestamp < sortedTimestamps[index - 1]; - }), - ).toBe(true); - }); +const defaultProps: SliceAdderProps = { + slices: mockSliceEntities.slices, + fetchSlices: jest.fn(), + updateSlices: jest.fn(), + selectedSliceIds: [127, 128], + userId: 1, + dashboardId: 0, + editMode: false, + errorMessage: '', + isLoading: false, + lastUpdated: 0, +}; + +const renderSliceAdder = (props = defaultProps) => + render( + <Provider store={mockStore}> Review Comment: same here - we dont need theme provider, and we can pass the mocked store through options, like `render(<Component />, { store: mockStore}` -- 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: notifications-unsubscr...@superset.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For additional commands, e-mail: notifications-h...@superset.apache.org