-
Notifications
You must be signed in to change notification settings - Fork 130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
converted editor_spec test to jest test fixes #772 #773
converted editor_spec test to jest test fixes #772 #773
Conversation
Also, the unfinished part is the part where an Ajax call was made. It looks like this:
I'm yet to figure out how to do it in puppeteer but I'm on it anyway. Any suggestion or guidance is welcome. |
Hi Paul - so this is called "mocking" - that is, pretending to receive a response from a server during tests. Here's a good guide for "mocking a jQuery AJAX call": https://stackoverflow.com/questions/44571207/mocking-jquery-ajax-with-jest Background on Jest Mock functions: https://jestjs.io/docs/mock-functions |
OK thank you, I'd check it out. |
OK, so i think we could just do this the really basic way, by manually mocking PublicLab.Editor/src/PublicLab.Editor.js Lines 109 to 121 in 1ab7b20
Here is the code inside of We could just make a new function inside the const $ = {
ajax: function(options) {
var d = $.Deferred(); // this is just creating a promise, i think. Maybe we could do it manually without jQuery, but this could also just work.
d.resolve(options);
d.reject(options);
return d.promise(); // this so that when the Editor code runs .done() and .fail() on the response, those are run on the promise.
}
} That would override jQuery's |
Try writing out this test and I can make suggestions inline! |
@jywarren but this line below is still trying to access But I came across this right here which is jest's way of mocking calls. I tried this below and the test passed but I don't know why it passes, I was expecting it to throw an error for the let spy = jest.fn().mockImplementation(async (options) => {
await page.evaluate(() => {
if(options === editor.options.destination) {
let d = $.Deferred();
d.resolve(options);
d.reject(options);
return d.promise();
}
});
function onPublish(response) {
expect(response).toBeDefined();
}
editor.publish(onPublish);
});
}); |
…DESIGNS/PublicLab.Editor into editor-jest-test-conversion pulled changes from remote editor-jest-test-conversion into local editor-jest-test-conversion
Just catching up here -- why did you expect the error for the $ symbol and on which line?
I like this. Is this what you ended up doing? Thanks! |
const spy = jest.fn().mockImplementation(async (options) => { | ||
await page.evaluate(() => { | ||
if (options === editor.options.destination) { | ||
const d = $.Deferred(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So here you expected it not to be able to use $
? I think it has access within the page context! Which is great.
expect( await page.evaluate(() => document.querySelector('.ple-textarea')) ).toBeDefined(); | ||
expect( await page.evaluateHandle(() => editor) ).toBeDefined(); | ||
expect( await page.evaluateHandle(() => editor.options.textarea) ).toBeDefined(); | ||
expect( await page.evaluateHandle(() => editor.options.textarea.innerHTML) ).toEqual(await page.evaluateHandle(() => document.querySelector('.ple-textarea').innerHTML)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like comparing strings worked here, re: expect(editor.options.textarea).toBe($('.ple-textarea')[0])
from your initial comment? I think this test looks good to go!
Hi guys, trust you had fun during the holidays.
This is a PR in progress but here's some information I discovered during the process.
page.evaluate()
function returns undefined for evaluations that are not asynchronous so during the test, jest receivesundefined
and not the evaluated value. To solve this I usedpage.evaluateHandle()
which returns the evaluated value if the evaluation is not an asynchronous process. More info hereI'm open to suggestions on how to handle this if there's a better way but currently, this works just fine.
editor_spec.js
jasmine file includes a test instanceexpect(editor.options.textarea).toBe($('.ple-textarea')[0])
which usually returnedfalse
when I tried to convert it to jest test because those both comparing factors returns a value that is a reference data type. In javascript,{state: true}
is not equal to{state: true}
. This, I think is the problem so currently, I've commented it out. Maybe I can compare their keys or values instead.@jywarren @TildaDares