|
| 1 | +import { AppFrontend } from 'test/e2e/pageobjects/app-frontend'; |
| 2 | + |
| 3 | +import type { ILayoutSettings } from 'src/layout/common.generated'; |
| 4 | +import type { ILayoutCollection } from 'src/layout/layout'; |
| 5 | + |
| 6 | +const appFrontend = new AppFrontend(); |
| 7 | + |
| 8 | +function fillTwoSubforms() { |
| 9 | + cy.findByRole('textbox', { name: /navn/i }).type('Per'); |
| 10 | + cy.findByRole('textbox', { name: /alder/i }).type('28'); |
| 11 | + |
| 12 | + cy.findByRole('button', { name: /legg til moped/i }).clickAndGone(); |
| 13 | + cy.findByRole('textbox', { name: /registreringsnummer/i }).type('ABC123'); |
| 14 | + cy.findByRole('textbox', { name: /merke/i }).type('Digdir'); |
| 15 | + cy.findByRole('textbox', { name: /modell/i }).type('Scooter2000'); |
| 16 | + cy.findByRole('textbox', { name: /produksjonsår/i }).type('2024'); |
| 17 | + cy.findByRole('button', { name: /ferdig/i }).clickAndGone(); |
| 18 | + |
| 19 | + cy.findByRole('button', { name: /legg til moped/i }).clickAndGone(); |
| 20 | + cy.findByRole('textbox', { name: /registreringsnummer/i }).type('XYZ987'); |
| 21 | + cy.findByRole('textbox', { name: /merke/i }).type('Altinn'); |
| 22 | + cy.findByRole('textbox', { name: /modell/i }).type('3.0'); |
| 23 | + cy.findByRole('textbox', { name: /produksjonsår/i }).type('2030'); |
| 24 | + cy.findByRole('button', { name: /ferdig/i }).clickAndGone(); |
| 25 | +} |
| 26 | + |
| 27 | +describe('Subform test', () => { |
| 28 | + beforeEach(() => { |
| 29 | + cy.startAppInstance(appFrontend.apps.subformTest, { authenticationLevel: '1' }); |
| 30 | + }); |
| 31 | + |
| 32 | + it('PDF should include subforms + single-subform PDFs should work', () => { |
| 33 | + fillTwoSubforms(); |
| 34 | + cy.testPdf({ |
| 35 | + snapshotName: 'subform', |
| 36 | + enableResponseFuzzing: true, |
| 37 | + returnToForm: true, |
| 38 | + callback: () => { |
| 39 | + cy.getSummary('Navn').should('contain.text', 'Per'); |
| 40 | + cy.getSummary('Alder').should('contain.text', '28 år'); |
| 41 | + |
| 42 | + cy.getSummary('Registreringsnummer').eq(0).should('contain.text', 'ABC123'); |
| 43 | + cy.getSummary('Merke').eq(0).should('contain.text', 'Digdir'); |
| 44 | + cy.getSummary('Modell').eq(0).should('contain.text', 'Scooter2000'); |
| 45 | + cy.getSummary('Produksjonsår').eq(0).should('contain.text', '2024'); |
| 46 | + |
| 47 | + cy.getSummary('Registreringsnummer').eq(1).should('contain.text', 'XYZ987'); |
| 48 | + cy.getSummary('Merke').eq(1).should('contain.text', 'Altinn'); |
| 49 | + cy.getSummary('Modell').eq(1).should('contain.text', '3.0'); |
| 50 | + cy.getSummary('Produksjonsår').eq(1).should('contain.text', '2030'); |
| 51 | + }, |
| 52 | + }); |
| 53 | + |
| 54 | + cy.findAllByRole('button', { name: /endre/i }).first().clickAndGone(); |
| 55 | + cy.findByRole('textbox', { name: /registreringsnummer/i }).should('have.value', 'ABC123'); |
| 56 | + |
| 57 | + cy.testPdf({ |
| 58 | + snapshotName: 'single-subform', |
| 59 | + enableResponseFuzzing: true, |
| 60 | + returnToForm: true, |
| 61 | + buildUrl: buildUrlForSingleSubform, |
| 62 | + callback: () => { |
| 63 | + cy.get('#moped-blurb').should( |
| 64 | + 'contain.text', |
| 65 | + 'Fyll inn dette skjemaet for å registrere en moped i din garasje', |
| 66 | + ); |
| 67 | + cy.getSummary('Navn').should('not.exist'); |
| 68 | + cy.getSummary('Alder').should('not.exist'); |
| 69 | + |
| 70 | + cy.getSummary('Registreringsnummer').should('contain.text', 'ABC123'); |
| 71 | + cy.getSummary('Registreringsnummer').should('not.contain.text', 'XYZ987'); |
| 72 | + cy.getSummary('Merke').should('contain.text', 'Digdir'); |
| 73 | + cy.getSummary('Modell').should('contain.text', 'Scooter2000'); |
| 74 | + cy.getSummary('Produksjonsår').should('contain.text', '2024'); |
| 75 | + }, |
| 76 | + }); |
| 77 | + |
| 78 | + cy.intercept('GET', '**/api/layoutsettings/moped-subform', (req) => { |
| 79 | + req.on('response', (res) => { |
| 80 | + const body = JSON.parse(res.body) as ILayoutSettings; |
| 81 | + body.pages.pdfLayoutName = 'moped-pdf'; // Forces PDF engine to use a tailor-made layout |
| 82 | + res.send(body); |
| 83 | + }); |
| 84 | + }).as('settings'); |
| 85 | + |
| 86 | + cy.testPdf({ |
| 87 | + snapshotName: 'single-subform-custom', |
| 88 | + enableResponseFuzzing: true, |
| 89 | + buildUrl: buildUrlForSingleSubform, |
| 90 | + callback: () => { |
| 91 | + cy.get('#moped-blurb').should('not.exist'); |
| 92 | + cy.getSummary('Navn').should('not.exist'); |
| 93 | + cy.getSummary('Alder').should('not.exist'); |
| 94 | + |
| 95 | + cy.getSummary('Registreringsnummer').should('contain.text', 'ABC123'); |
| 96 | + cy.getSummary('Registreringsnummer').should('not.contain.text', 'XYZ987'); |
| 97 | + cy.getSummary('Merke').should('contain.text', 'Digdir'); |
| 98 | + cy.getSummary('Modell').should('contain.text', 'Scooter2000'); |
| 99 | + cy.getSummary('Produksjonsår').should('contain.text', '2024'); |
| 100 | + }, |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should render PDF with summary2 layoutset with subform and subform table', () => { |
| 105 | + const pdfLayoutName = 'CustomPDF'; |
| 106 | + cy.intercept('GET', '**/layoutsettings/**', (req) => |
| 107 | + req.on('response', (res) => { |
| 108 | + const body: ILayoutSettings = JSON.parse(res.body); |
| 109 | + res.send({ |
| 110 | + ...body, |
| 111 | + pages: { ...body.pages, pdfLayoutName }, |
| 112 | + }); |
| 113 | + }), |
| 114 | + ); |
| 115 | + |
| 116 | + cy.intercept('GET', '**/layouts/**', (req) => |
| 117 | + req.on('response', (res) => { |
| 118 | + const body: ILayoutCollection = JSON.parse(res.body); |
| 119 | + res.send({ |
| 120 | + ...body, |
| 121 | + [pdfLayoutName]: { |
| 122 | + data: { |
| 123 | + layout: [ |
| 124 | + { |
| 125 | + id: 'title', |
| 126 | + type: 'Header', |
| 127 | + textResourceBindings: { title: 'This is a custom PDF' }, |
| 128 | + size: 'L', |
| 129 | + }, |
| 130 | + { |
| 131 | + id: 'summary2-layoutset', |
| 132 | + type: 'Summary2', |
| 133 | + target: { |
| 134 | + taskId: 'Task_1', |
| 135 | + type: 'layoutSet', |
| 136 | + }, |
| 137 | + showPageInAccordion: false, |
| 138 | + overrides: [ |
| 139 | + { |
| 140 | + componentId: 'subform-mopeder', |
| 141 | + display: 'table', |
| 142 | + }, |
| 143 | + ], |
| 144 | + }, |
| 145 | + ], |
| 146 | + }, |
| 147 | + }, |
| 148 | + }); |
| 149 | + }), |
| 150 | + ); |
| 151 | + |
| 152 | + cy.waitUntilSaved(); |
| 153 | + fillTwoSubforms(); |
| 154 | + |
| 155 | + cy.testPdf({ |
| 156 | + snapshotName: 'subform', |
| 157 | + enableResponseFuzzing: true, |
| 158 | + callback: () => { |
| 159 | + cy.getSummary('Navn').should('contain.text', 'Per'); |
| 160 | + cy.getSummary('Alder').should('contain.text', '28 år'); |
| 161 | + |
| 162 | + cy.findByRole('columnheader', { name: 'Regnummer' }); |
| 163 | + cy.findByRole('columnheader', { name: 'Merke' }); |
| 164 | + cy.findByRole('columnheader', { name: 'Ekstra info' }); |
| 165 | + |
| 166 | + cy.findByRole('cell', { name: 'ABC123' }); |
| 167 | + cy.findByRole('cell', { name: 'Digdir' }); |
| 168 | + cy.findByRole('cell', { name: 'XYZ987' }); |
| 169 | + cy.findByRole('cell', { name: 'Altinn' }); |
| 170 | + }, |
| 171 | + }); |
| 172 | + }); |
| 173 | +}); |
| 174 | + |
| 175 | +function buildUrlForSingleSubform(href: string) { |
| 176 | + if (!href.includes('/utfylling/') && !href.includes('/whatever/')) { |
| 177 | + // We replace this with 'whatever' to make sure we can still load the PDF whatever the main page |
| 178 | + // name is. It should not matter inside this subform. |
| 179 | + throw new Error('Expected URL to contain /utfylling/ but it was not found'); |
| 180 | + } |
| 181 | + |
| 182 | + if (!href.endsWith('/moped-utfylling')) { |
| 183 | + throw new Error('Expected URL to end with /moped-utfylling but it was not found'); |
| 184 | + } |
| 185 | + |
| 186 | + return href.replace('/utfylling/', '/whatever/').replace('/moped-utfylling', '/?pdf=1'); |
| 187 | +} |
0 commit comments