Skip to content

Commit

Permalink
display the the experiment's JSON data in a new window (#53)
Browse files Browse the repository at this point in the history
* display the the experiment's JSON data in a new window

* fixed test
  • Loading branch information
bbonf authored May 24, 2024
1 parent 951814f commit dc57f77
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
7 changes: 4 additions & 3 deletions jspsych-uil-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<!doctype html><html><body><h1>Experiment Data (debug version)</h1>${pre_element.outerHTML}</body></html>`;
let url = URL.createObjectURL(new Blob([content], {type: 'text/html;charset=utf-8'}));
window.open(url);
}
}

Expand Down
11 changes: 7 additions & 4 deletions spec/utils.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit dc57f77

Please sign in to comment.