From dc57f77d423a0fc59cfb691fece3486be9bb3af7 Mon Sep 17 00:00:00 2001 From: Ben Bonfil Date: Fri, 24 May 2024 09:47:21 +0200 Subject: [PATCH] display the the experiment's JSON data in a new window (#53) * display the the experiment's JSON data in a new window * fixed test --- jspsych-uil-utils.js | 7 ++++--- spec/utils.spec.mjs | 11 +++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/jspsych-uil-utils.js b/jspsych-uil-utils.js index 4fd1b73..de5c206 100644 --- a/jspsych-uil-utils.js +++ b/jspsych-uil-utils.js @@ -405,12 +405,13 @@ function saveJson (json, access_key, acc_server = undefined) { else { // show the data in prettyfied format json = JSON.stringify(JSON.parse(json), null, 4); - // clear the body - document.body.innerHTML= ''; // Add preformatted json content. let pre_element = document.createElement("pre"); pre_element.innerText = json; - document.body.append(pre_element); + + let content = `

Experiment Data (debug version)

${pre_element.outerHTML}`; + let url = URL.createObjectURL(new Blob([content], {type: 'text/html;charset=utf-8'})); + window.open(url); } } diff --git a/spec/utils.spec.mjs b/spec/utils.spec.mjs index 3308556..b156286 100644 --- a/spec/utils.spec.mjs +++ b/spec/utils.spec.mjs @@ -63,12 +63,15 @@ describe('saveJson', () => { expect(request.params).toBe(JSON.stringify(data)); }); - it('should display json when offline', () => { - let mockBody = document.createElement('body'); - spyOnProperty(document, 'body').and.returnValue(mockBody); + it('should display json when offline', async () => { + let open = spyOn(window, 'open'); + utils.saveJson(JSON.stringify(data), key); - let parsed = JSON.parse(mockBody.innerText); + expect(open).toHaveBeenCalled(); + let blob = await fetch(open.calls.first().args[0], {headers: {'content-type': 'text/html;charset=utf-8'}}); + let dom = new DOMParser().parseFromString(await blob.text(), 'text/html'); + let parsed = JSON.parse(dom.querySelector('pre').innerText); expect(parsed).toEqual(data); }); });