Skip to content
Merged
30 changes: 29 additions & 1 deletion packages/vue-components/src/__tests__/Quiz.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mount } from '@vue/test-utils';
import { config, mount } from '@vue/test-utils';
import Quiz from '../questions/Quiz.vue';
import Question from '../questions/Question.vue';
import QOption from '../questions/QOption.vue';
Expand All @@ -10,6 +10,11 @@ import QOption from '../questions/QOption.vue';
- Score screen
*/

// Prevent default transition stubs to allow @after-leave to be triggered
config.stubs = {
transition: false,
};

const DEFAULT_STUBS = {
'question': Question,
'q-option': QOption,
Expand Down Expand Up @@ -51,6 +56,12 @@ const BLANKS_QUESTION = `
</question>
`;

// Function to delay test until question is rendered
function timeoutRenderQuestion() {
// eslint-disable-next-line no-promise-executor-return
return new Promise(resolve => setTimeout(resolve, 350));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there no way to work around using a timeout?

Something like waitForSelector(...).toGoMissing() (imaginary).
Or creating your own simple transition stub.

There are 17 calls here so resolving this should help improve the speed of tests a fair bit. Unless the runner is parallel, I can't remember..

But don't spend too much time on it, 6s to me is still an ok degradation for now. (just create a follow up issue if we can't resolve this).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jovyntls could you help to merge in this MR if it looks good to you? 🙇

}

describe('Quizzes', () => {
test('intro screen with intro text renders correctly', () => {
const wrapper = mount(Quiz, {
Expand All @@ -75,11 +86,13 @@ describe('Quizzes', () => {
// Click 'begin'
await wrapper.find('button').trigger('click');

await timeoutRenderQuestion();
// Click option 1 of mcq question
await wrapper.findAllComponents(QOption).at(0).find('div').trigger('click');
// Click check of mcq question
await wrapper.find('button.btn-primary').trigger('click');

await timeoutRenderQuestion();
expect(wrapper.element).toMatchSnapshot();
});

Expand All @@ -94,12 +107,14 @@ describe('Quizzes', () => {
// Click 'begin'
await wrapper.find('button').trigger('click');

await timeoutRenderQuestion();
// Click options 1 & 2 of checkbox question
await wrapper.findAllComponents(QOption).at(0).find('div').trigger('click');
await wrapper.findAllComponents(QOption).at(1).find('div').trigger('click');
// Click check of checkbox question
await wrapper.find('button.btn-primary').trigger('click');

await timeoutRenderQuestion();
expect(wrapper.element).toMatchSnapshot();
});

Expand All @@ -114,11 +129,13 @@ describe('Quizzes', () => {
// click 'begin'
await wrapper.find('button').trigger('click');

await timeoutRenderQuestion();
// Type incorrect answer into blank 2 of blanks question
await wrapper.findAllComponents(QOption).at(1).find('input').setValue('wrong');
// Click check of blanks question
await wrapper.find('button.btn-primary').trigger('click');

await timeoutRenderQuestion();
expect(wrapper.element).toMatchSnapshot();
});

Expand All @@ -133,11 +150,13 @@ describe('Quizzes', () => {
// Click 'begin'
await wrapper.find('button').trigger('click');

await timeoutRenderQuestion();
// Type answer into textarea of text question
await wrapper.find('textarea').setValue('abc lorem ipsum');
// Click 'check' of text question
await wrapper.find('button.btn-primary').trigger('click');

await timeoutRenderQuestion();
expect(wrapper.element).toMatchSnapshot();
});

Expand All @@ -152,20 +171,23 @@ describe('Quizzes', () => {
// Click 'begin'
await wrapper.find('button').trigger('click');

await timeoutRenderQuestion();
// Click option 2 of mcq question
await wrapper.findAllComponents(QOption).at(1).find('div').trigger('click');
// Click check of mcq question
await wrapper.findComponent(Question).find('button.btn-primary').trigger('click');
// Click next
await wrapper.find('button.btn-primary').trigger('click');

await timeoutRenderQuestion();
// Type answer into textarea of text question
await wrapper.find('textarea').setValue('abc lorem ipsum');
// Click 'check' of text question
await wrapper.find('button.btn-primary').trigger('click');
// Click next
await wrapper.find('button.btn-primary').trigger('click');

await timeoutRenderQuestion();
// Click options 1 & 3 of checkbox question
await wrapper.findAllComponents(QOption).at(0).find('div').trigger('click');
await wrapper.findAllComponents(QOption).at(2).find('div').trigger('click');
Expand All @@ -174,13 +196,15 @@ describe('Quizzes', () => {
// Click next
await wrapper.find('button.btn-primary').trigger('click');

await timeoutRenderQuestion();
// Type correct answer into blank 2 of blanks question
await wrapper.findAllComponents(QOption).at(1).find('input').setValue('key');
// Click check of checkbox question
await wrapper.find('button.btn-primary').trigger('click');
// Click next
await wrapper.find('button.btn-primary').trigger('click');

await timeoutRenderQuestion();
expect(wrapper.element).toMatchSnapshot();
});

Expand All @@ -195,23 +219,27 @@ describe('Quizzes', () => {
// Click 'begin'
await wrapper.find('button').trigger('click');

await timeoutRenderQuestion();
// Click option 2 of mcq question
await wrapper.findAllComponents(QOption).at(1).find('div').trigger('click');
// Click check of mcq question
await wrapper.findComponent(Question).find('button.btn-primary').trigger('click');
// Click next
await wrapper.find('button.btn-primary').trigger('click');

await timeoutRenderQuestion();
// Type answer into textarea of text question
await wrapper.find('textarea').setValue('abc lorem ipsum');
// Click 'check' of text question
await wrapper.find('button.btn-primary').trigger('click');
// Click next
await wrapper.find('button.btn-primary').trigger('click');

await timeoutRenderQuestion();
// Click retry
await wrapper.find('button.btn-outline-primary').trigger('click');

await timeoutRenderQuestion();
expect(wrapper.element).toMatchSnapshot();
});
});
Loading