Skip to content

Commit

Permalink
Make sure options are loaded in PDF (#2958)
Browse files Browse the repository at this point in the history
* fix pdf testing so it actually times out and add test for slow option loading

* check if options state is ready
  • Loading branch information
bjosttveit authored Jan 31, 2025
1 parent 1f3770c commit b489108
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
15 changes: 14 additions & 1 deletion src/features/options/OptionsPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import type { IOptionInternal } from 'src/features/options/castOptionsToStrings'
import type { OptionsValueType } from 'src/features/options/useGetOptions';
import type { ISelectionComponent, ISelectionComponentFull } from 'src/layout/common.generated';
import type { CompTypes } from 'src/layout/layout';
import type { DefPluginExtraState, DefPluginStateFactoryProps } from 'src/utils/layout/plugins/NodeDefPlugin';
import type { NodesContext } from 'src/utils/layout/NodesContext';
import type {
DefPluginExtraState,
DefPluginState,
DefPluginStateFactoryProps,
} from 'src/utils/layout/plugins/NodeDefPlugin';

interface Config<SupportsPreselection extends boolean> {
componentType: CompTypes;
Expand Down Expand Up @@ -71,4 +76,12 @@ export class OptionsPlugin<E extends ExternalConfig> extends NodeDefPlugin<ToInt
allowEffects={${allowsEffects ? 'true' : 'false'}}
/>`.trim();
}

stateIsReady(state: DefPluginState<ToInternal<E>>, fullState: NodesContext): boolean {
if (!super.stateIsReady(state, fullState)) {
return false;
}

return !!state.options;
}
}
20 changes: 20 additions & 0 deletions test/e2e/integration/frontend-test/pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,26 @@ describe('PDF', () => {
});
});

it('options should load before #readyForPrint is shown', () => {
cy.goto('changename');
cy.dsSelect(appFrontend.changeOfName.sources, 'Digitaliseringsdirektoratet');
cy.dsSelect(appFrontend.changeOfName.reference, 'Sophie Salt');
cy.dsSelect(appFrontend.changeOfName.reference2, 'Dole');

cy.intercept(/.*\/options\/(list|references|test).*/, async (r) => {
await new Promise((resolve) => setTimeout(resolve, 3000));
r.continue();
});

cy.testPdf({
callback: () => {
cy.getSummary('hvor fikk du vite om skjemaet').should('contain.text', 'Digitaliseringsdirektoratet');
cy.getSummary('Referanse').should('contain.text', 'Sophie Salt');
cy.getSummary('Referanse 2').should('contain.text', 'Dole');
},
});
});

it('should generate PDF for group step', () => {
cy.goto('group');
cy.findByRole('checkbox', { name: /liten/i }).check();
Expand Down
23 changes: 13 additions & 10 deletions test/e2e/support/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { getInstanceIdRegExp } from 'src/utils/instanceIdRegExp';
import type { LayoutContextValue } from 'src/features/form/layout/LayoutsContext';
import JQueryWithSelector = Cypress.JQueryWithSelector;

import { getTargetUrl } from 'test/e2e/support/start-app-instance';

import type { ILayoutFile } from 'src/layout/common.generated';

const appFrontend = new AppFrontend();
Expand Down Expand Up @@ -597,7 +599,7 @@ Cypress.Commands.add('directSnapshot', (snapshotName, { width, minHeight }, rese
</html>`,
),
);
cy.visit('/screenshot');
cy.visit(getTargetUrl('screenshot'));

cy.percySnapshot(snapshotName, { widths: [width], minHeight });

Expand All @@ -608,7 +610,6 @@ Cypress.Commands.add('directSnapshot', (snapshotName, { width, minHeight }, rese
}
});

const DEFAULT_COMMAND_TIMEOUT = Cypress.config().defaultCommandTimeout;
Cypress.Commands.add('testPdf', ({ snapshotName = false, beforeReload, callback, returnToForm = false }) => {
cy.log('Testing PDF');

Expand Down Expand Up @@ -662,14 +663,16 @@ Cypress.Commands.add('testPdf', ({ snapshotName = false, beforeReload, callback,
cy.viewport(794, 1123);
cy.get('body').invoke('css', 'margin', '0.75in');

cy.then(() => Cypress.config('defaultCommandTimeout', 0));

// Verify that generic elements that should be hidden are not present
cy.findAllByRole('button').should('not.exist');
// Run tests from callback
callback();

cy.then(() => Cypress.config('defaultCommandTimeout', DEFAULT_COMMAND_TIMEOUT));
cy.then(() => {
const timeout = setTimeout(() => {
throw 'PDF callback failed, print was not ready when #readyForPrint appeared';
}, 0);
// Verify that generic elements that should be hidden are not present
cy.findAllByRole('button').should('not.exist');
// Run tests from callback
callback();
cy.then(() => clearTimeout(timeout));
});

if (snapshotName) {
// Take snapshot of PDF
Expand Down
2 changes: 0 additions & 2 deletions test/e2e/support/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ before(() => {
chai.use(chaiExtensions);
});

const DEFAULT_COMMAND_TIMEOUT = Cypress.config().defaultCommandTimeout;
// Clear media emulation and reset default command timeout before each test
beforeEach(() => {
cy.then(() => Cypress.config('defaultCommandTimeout', DEFAULT_COMMAND_TIMEOUT));
cy.wrap(
Cypress.automation('remote:debugger:protocol', {
command: 'Emulation.setEmulatedMedia',
Expand Down

0 comments on commit b489108

Please sign in to comment.