From 03f5171fe068c39609c66cb209443d211806d4f2 Mon Sep 17 00:00:00 2001 From: Adam Raine Date: Thu, 26 May 2022 09:55:57 -0700 Subject: [PATCH 1/2] tests(topbar): remove testdouble --- flow-report/src/topbar.tsx | 12 ++++++++++-- flow-report/test/topbar-test.tsx | 12 ++++-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/flow-report/src/topbar.tsx b/flow-report/src/topbar.tsx index 0515164d8ac5..5d6d74753527 100644 --- a/flow-report/src/topbar.tsx +++ b/flow-report/src/topbar.tsx @@ -17,9 +17,12 @@ import {saveFile} from '../../report/renderer/api'; function saveHtml(flowResult: LH.FlowResult, htmlStr: string) { const blob = new Blob([htmlStr], {type: 'text/html'}); const filename = getFlowResultFilenamePrefix(flowResult) + '.html'; - saveFile(blob, filename); + saveHtml.saveFile(blob, filename); } +// Store `saveFile` here so we can do dependency injection. +saveHtml.saveFile = saveFile; + /* eslint-disable max-len */ const Logo: FunctionComponent = () => { return ( @@ -68,7 +71,7 @@ const TopbarButton: FunctionComponent<{ ); }; -export const Topbar: FunctionComponent<{onMenuClick: JSX.MouseEventHandler}> = +const Topbar: FunctionComponent<{onMenuClick: JSX.MouseEventHandler}> = ({onMenuClick}) => { const flowResult = useFlowResult(); const strings = useLocalizedStrings(); @@ -118,3 +121,8 @@ export const Topbar: FunctionComponent<{onMenuClick: JSX.MouseEventHandler ); }; + +export { + Topbar, + saveHtml, +}; diff --git a/flow-report/test/topbar-test.tsx b/flow-report/test/topbar-test.tsx index d9a9fabf688e..8a6005a4ce69 100644 --- a/flow-report/test/topbar-test.tsx +++ b/flow-report/test/topbar-test.tsx @@ -10,16 +10,10 @@ import {act, render} from '@testing-library/preact'; import {FlowResultContext, OptionsContext} from '../src/util'; import {I18nProvider} from '../src/i18n/i18n'; +import {Topbar, saveHtml} from '../src/topbar'; const mockSaveFile = jest.fn(); -jest.unstable_mockModule('../../../report/renderer/api.js', () => ({ - saveFile: mockSaveFile, -})); - -let Topbar: typeof import('../src/topbar').Topbar; -beforeAll(async () => { - Topbar = (await import('../src/topbar')).Topbar; -}); +const defaultSaveFile = saveHtml.saveFile; const flowResult = { name: 'User flow', @@ -34,6 +28,7 @@ let wrapper: FunctionComponent; let options: LH.FlowReportOptions; beforeEach(() => { + saveHtml.saveFile = defaultSaveFile; mockSaveFile.mockReset(); options = {}; wrapper = ({children}) => ( @@ -48,6 +43,7 @@ beforeEach(() => { }); it('save button opens save dialog for HTML file', async () => { + saveHtml.saveFile = mockSaveFile; options = {getReportHtml: () => ''}; const root = render( {}}/>, {wrapper}); From 7f135f9b64a261ea3a41a38ce830b4bcc0775661 Mon Sep 17 00:00:00 2001 From: Adam Raine Date: Thu, 26 May 2022 12:58:34 -0700 Subject: [PATCH 2/2] afterEach --- flow-report/test/topbar-test.tsx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/flow-report/test/topbar-test.tsx b/flow-report/test/topbar-test.tsx index 8a6005a4ce69..b03c0178b4ab 100644 --- a/flow-report/test/topbar-test.tsx +++ b/flow-report/test/topbar-test.tsx @@ -28,7 +28,6 @@ let wrapper: FunctionComponent; let options: LH.FlowReportOptions; beforeEach(() => { - saveHtml.saveFile = defaultSaveFile; mockSaveFile.mockReset(); options = {}; wrapper = ({children}) => ( @@ -42,6 +41,10 @@ beforeEach(() => { ); }); +afterEach(() => { + saveHtml.saveFile = defaultSaveFile; +}); + it('save button opens save dialog for HTML file', async () => { saveHtml.saveFile = mockSaveFile; options = {getReportHtml: () => ''};