Kunal8954 commented on code in PR #44154: URL: https://github.com/apache/superset/pull/44154#discussion_r4078692268
########## superset-frontend/src/utils/downloadAsPdf.test.ts: ########## @@ -0,0 +1,98 @@ +/** + * 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 downloadAsPdf from './downloadAsPdf'; + +jest.mock('dom-to-pdf', () => ({ + __esModule: true, + default: jest.fn(), +})); + +jest.mock('src/utils/getBootstrapData', () => ({ + __esModule: true, + default: jest.fn(() => ({ common: { pdf_compression_level: 'NONE' } })), +})); + +jest.mock('@apache-superset/core/translation', () => ({ + t: (str: string) => str, +})); + +jest.mock('@apache-superset/core/utils', () => ({ + logging: { error: jest.fn() }, +})); + +jest.mock('./downloadUtils', () => ({ + forceLoadAllCharts: jest.fn().mockResolvedValue(false), + restoreVirtualization: jest.fn(), +})); + +afterEach(() => { + jest.restoreAllMocks(); + // `restoreAllMocks` only restores `jest.spyOn` spies; `domToPdf` is a + // module mock, so a per-test `mockRejectedValue`/`mockResolvedValue` + // would otherwise leak into later tests. Reset it explicitly. + (jest.requireMock('dom-to-pdf').default as jest.Mock).mockReset(); +}); + +test('warns the user via the bound toast callback and returns early when the target element is not found', async () => { + jest.spyOn(document, 'querySelector').mockReturnValue(null); + const addWarningToast = jest.fn(); + const domToPdf = jest.requireMock('dom-to-pdf').default as jest.Mock; + + await downloadAsPdf( + '.non-existent-selector', + 'test-file', + true, + addWarningToast, + )({} as any); + + // Passed in already bound to dispatch (e.g. via `useToasts()`), so calling + // it directly is what actually renders the toast -- unlike the raw action + // creator, which only builds a Redux action object. + expect(addWarningToast).toHaveBeenCalledWith( + 'PDF download failed, please refresh and try again.', + ); + expect(domToPdf).not.toHaveBeenCalled(); +}); + +test('does not throw when the target element is not found and no toast callback is provided', async () => { + jest.spyOn(document, 'querySelector').mockReturnValue(null); + + await expect( + downloadAsPdf('.non-existent-selector', 'test-file', true)({} as any), + ).resolves.not.toThrow(); Review Comment: Thanks for the catch! toThrow only accepts a function and the handler resolves to undefined, so that assertion wasn't actually testing anything. I switched it to assert the resolved value directly — await expect(...).resolves.toBeUndefined() — and the focused suite still passes. Fix is in 443f9ab57f on my fork if someone here wants to cherry-pick it. -- 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]
