From 5d6bcfb67c667a1182183933f2fc09d1b080bf0e Mon Sep 17 00:00:00 2001 From: ix5 Date: Tue, 24 May 2022 00:49:33 +0200 Subject: [PATCH 1/3] [nit] js: jest-unit: Add trailing comma --- isso/js/jest-unit.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/isso/js/jest-unit.config.js b/isso/js/jest-unit.config.js index 1c3e26690..7da7d85a0 100644 --- a/isso/js/jest-unit.config.js +++ b/isso/js/jest-unit.config.js @@ -39,7 +39,7 @@ const config = { */ //testEnvironment: "jsdom", - "globalSetup": "/tests/setup/global-setup.js" + "globalSetup": "/tests/setup/global-setup.js", }; module.exports = config; From c867d3f26cf257a5f3586adf4974af81bc331810 Mon Sep 17 00:00:00 2001 From: ix5 Date: Tue, 24 May 2022 00:53:48 +0200 Subject: [PATCH 2/3] js: test: Add boilerplate jest-puppeteer conf Currently unused, but useful for quick debugging & adjustments during E2E testing. --- isso/js/jest-integration.config.js | 4 ++++ isso/js/jest-puppeteer.config.js | 14 ++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 isso/js/jest-puppeteer.config.js diff --git a/isso/js/jest-integration.config.js b/isso/js/jest-integration.config.js index ee3584a29..a62214292 100644 --- a/isso/js/jest-integration.config.js +++ b/isso/js/jest-integration.config.js @@ -8,6 +8,10 @@ * https://jestjs.io/docs/configuration */ +// https://github.com/smooth-code/jest-puppeteer/issues/160#issuecomment-491975158 +// For `jest-puppeteer` package, currently empty but good to have +process.env.JEST_PUPPETEER_CONFIG = require.resolve('./jest-puppeteer.config.js'); + const config = { /* puppeteer end-to-end integration testing * https://jestjs.io/docs/configuration#preset-string */ diff --git a/isso/js/jest-puppeteer.config.js b/isso/js/jest-puppeteer.config.js new file mode 100644 index 000000000..fe8a6d71c --- /dev/null +++ b/isso/js/jest-puppeteer.config.js @@ -0,0 +1,14 @@ +/* Configuration specific to `jest-puppeteer`, for E2E integration tests + * Could be used to set e.g. Firefox as test runner + * https://github.com/smooth-code/jest-puppeteer */ + +/* +module.exports = { + launch: { + dumpio: true, + headless: process.env.HEADLESS !== 'false', + product: 'chrome', + }, + browserContext: 'default', +} +*/ From 79979a4192a714a9a561d953a1f0246502732f1b Mon Sep 17 00:00:00 2001 From: ix5 Date: Wed, 25 May 2022 06:30:56 +0200 Subject: [PATCH 3/3] js: test/integration: Test API directly --- isso/js/tests/integration/puppet.test.js | 105 ++++++++++++++++++++++- 1 file changed, 104 insertions(+), 1 deletion(-) diff --git a/isso/js/tests/integration/puppet.test.js b/isso/js/tests/integration/puppet.test.js index ab419c6d8..9748c8878 100644 --- a/isso/js/tests/integration/puppet.test.js +++ b/isso/js/tests/integration/puppet.test.js @@ -15,11 +15,17 @@ * $ isso -c contrib/isso-dev.cfg run */ + const ISSO_ENDPOINT = process.env.ISSO_ENDPOINT ? process.env.ISSO_ENDPOINT : 'http://localhost:8080'; // Reset page before each test beforeEach(async () => { + await page.setViewport({ + width: 1920, + height: 1080, + deviceScaleFactor: 1, + }); await page.goto( ISSO_ENDPOINT + '/demo', { waitUntil: 'load' } @@ -34,7 +40,7 @@ beforeEach(async () => { //await jestPuppeteer.debug() //await jestPuppeteer.resetBrowser() //await jestPuppeteer.resetPage() -}) +}); test('window.Isso functions should be idempotent', async () => { @@ -148,3 +154,100 @@ test("should fill Postbox with valid data and receive 201 reply", async () => { // Need to click once to surface "confirm" and then again to confirm await expect(page).toClick('#isso-1 > .isso-text-wrapper > .isso-comment-footer > .isso-delete'); }); + +test("should execute GET/PUT/POST/DELETE requests correctly", async () => { + + let newComment = { + author: "Some name", + email: "test@test.test", + notification: 0, + parent: null, + text: "A comment", + title: "Isso Test", + website: null, + }; + + // Create comment via POST + await page.setRequestInterception(true); + let createHandler = (request) => { + let data = { + 'method': 'POST', + 'postData': JSON.stringify(newComment), + 'headers': { + ...request.headers(), + 'Content-Type': 'application/json', + }, + }; + request.continue(data); + page.setRequestInterception(false); + page.off('request', createHandler); + }; + await page.on('request', createHandler); + await page.goto(ISSO_ENDPOINT + '/new?uri=%2Fdemo%2Findex.html'); + + // Reload page to inspect new/changed/deleted comments + await page.goto( + ISSO_ENDPOINT + '/demo', + { waitUntil: 'load' } + ); + + // Relies on cookies from page.cookies, sent automatically + let postData = { + //id: 1, + text: 'New comment body', + author: 'Commenter #2', + website: 'https://new.website', + }; + + // Edit comment via PUT + await page.setRequestInterception(true); + let editHandler = request => { + let data = { + 'method': 'PUT', + 'postData': JSON.stringify(postData), + 'headers': { + ...request.headers(), + 'Content-Type': 'application/json', + }, + }; + request.continue(data); + page.off('request', editHandler); + page.setRequestInterception(false); + }; + await page.on('request', editHandler); + await page.goto(ISSO_ENDPOINT + '/id/1'); + + // Reload page to inspect new/changed/deleted comments + await page.goto( + ISSO_ENDPOINT + '/demo', + { waitUntil: 'load' } + ); + + await expect(page).toMatchElement( + '#isso-1 .isso-text', + { text: 'New comment body' }, + ); + + // Delete comment via DELETE + await page.setRequestInterception(true); + let deleteHandler = request => { + let data = { + 'method': 'DELETE', + //'postData': JSON.stringify({id: 1}), + 'headers': { + ...request.headers(), + 'Content-Type': 'application/json', + }, + }; + request.continue(data); + page.off('request', deleteHandler); + page.setRequestInterception(false); + }; + await page.once('request', deleteHandler); + await page.goto(ISSO_ENDPOINT + '/id/1'); + + await expect(page).not.toMatchElement( + '#isso-1 .isso-text', + { text: 'New comment body' }, + ); +});