swsnu / swppfall2021

Learning Software Engineering By Building Web Services
28 stars 19 forks source link

[hw 3-2] questions about simulating window functions #100

Open yeppin opened 3 years ago

yeppin commented 3 years ago

I have a question regarding simulating user's action in window.confirm and window.prompt function. So far I wrote tests which check whether those functions have been called or not. However I would like to handle further testing with the returned value from window.confirm / window.prompt.

These are the examples of codes that need to be tested,

if (window.confirm('confirm description.')){
// function execution
};
let variableA = prompt('prompt description');
if (variableA){
// function execution
};

I have been trying by using spyOn operation to window functions with .mockImplementation and .mockReturnedValue. However as the functions become fake, its value could not cover up the actual codes that execute with actual return value. is there a way to simulate window functions and test with its returned value?

Thank you

chang-jin commented 3 years ago

I am not sure I got your point correctly, but although the functions become fake(mock) with spyOn, you could cover up the actual codes by modifying its return value. like, with jest.spyOn(window, 'confirm').mockImplementation(() => true); you can test execution inside of the function for

if (window.confirm('confirm description.')){
// function execution
};

Same for window.prompt as well. Please let us know if we misunderstood your point

yeppin commented 3 years ago

Thanks to the reply, I think I found the answer. Thank you!