Skip to content

Commit

Permalink
[GH-788] Added a test case to prove that #788 has been fixed (this fa…
Browse files Browse the repository at this point in the history
…ils prior to f2fe81f)
  • Loading branch information
eddie.stanley committed Jun 9, 2022
1 parent f2fe81f commit 57dbf5c
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions tests/test-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -2649,3 +2649,52 @@ describe('Custom Tests', function() {
generateTest(CUSTOM_TESTS[i]);
}
});

describe("Asynchronous tests", () => {
it("When parsing synchronously inside a web-worker not owned by PapaParse we should not invoke postMessage", async () => {
// Arrange
const papaParseScriptPath = new URL("../papaparse.js", document.baseURI).href;

// Define our custom web-worker that loads PapaParse and executes a synchronous parse
const blob = new Blob([
`
importScripts('${papaParseScriptPath}');
self.addEventListener("message", function(event) {
if (event.data === "ExecuteParse") {
// Perform our synchronous parse, as requested
const results = Papa.parse('x\\ny\\n');
postMessage({type: "ParseExecutedSuccessfully", results});
} else {
// Otherwise, send whatever we received back. We shouldn't be hitting this (!) If we're reached
// this it means PapaParse thinks it is running inside a web-worker that it owns
postMessage(event.data);
}
});
`
], {type: 'text/javascript'});

const blobURL = window.URL.createObjectURL(blob);
const webWorker = new Worker(blobURL);

const receiveMessagePromise = new Promise((resolve, reject) => {
webWorker.addEventListener("message", event => {
if (event.data.type === "ParseExecutedSuccessfully") {
resolve(event.data);
} else {
const error = new Error(`Received unexpected message: ${JSON.stringify(event.data, null, 2)}`);
error.data = event.data;
reject(error);
}
});
});

// Act
webWorker.postMessage("ExecuteParse");
const webWorkerMessage = await receiveMessagePromise;

// Assert
assert.equal("ParseExecutedSuccessfully", webWorkerMessage.type);
assert.equal(3, webWorkerMessage.results.data.length);
});
});

0 comments on commit 57dbf5c

Please sign in to comment.