maoberlehner / perfundo

a pure CSS lightbox (now with JavaScript).
https://perfundo.oberlehner.net
MIT License
185 stars 9 forks source link

Remove unnecessary await from TestCafe tests #98

Closed AlexanderMoskovkin closed 7 years ago

AlexanderMoskovkin commented 7 years ago

Hi @maoberlehner,

At first, thanks for you article about testing with TestCafe.

In this pull request I've updated you tests and removed unnecessary awaits. Let me explain why I did it and how does it work.

Selector('#id') - it's a constructor that creates an instance of Selector. This instance exposes async methods and property getters to get element state:

const selector = Selector('#id');

await selector.exists; // -> true

TestCafe Assertions allows to pass this async methods and props as an actual value (without await):

await t.expect(selector.exists).ok();

We recommend to use this way because it enables the Smart Assertion Query Mechanism.

If you'd like to get an element snaphot that contains all property values exposed by the selector in a single object you need to call the selector with the await keyword like you would do with regular async functions:

var container = Selector('#base');
var containerSnapshot = await container();

console.log(containerSnapshot.childElementCount); // => <number>

or

var containerSnapshot = await Selector('#base')();

console.log(containerSnapshot.childElementCount); // => <number>
maoberlehner commented 7 years ago

Thank you for taking the time fixing this and explaining it to me – I really appreciate it.

I've also updated the demo code in my article.