Skip to content

Commit 55eb385

Browse files
committed
module 7 tests
1 parent d08bb7a commit 55eb385

4 files changed

Lines changed: 204 additions & 0 deletions
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import App from '../../App';
4+
import { shallow } from 'enzyme';
5+
import { assert } from 'chai';
6+
import sinon from 'sinon';
7+
8+
let quizEndComponentExists = false;
9+
let QuizEnd;
10+
try {
11+
QuizEnd = require('../../QuizEnd.js').default;
12+
quizEndComponentExists = true;
13+
} catch (e) {
14+
quizEndComponentExists = false;
15+
}
16+
17+
describe('QuizEnd Component', () => {
18+
it('has an anchor tag with an onClick handler @quiz-end-component-anchor-has-onclick-handler', () => {
19+
assert(quizEndComponentExists, "The QuizEnd component hasn't been created yet.")
20+
21+
let spy
22+
try {
23+
spy = sinon.spy(QuizEnd.prototype, 'handleResetClick')
24+
} catch (e) {
25+
assert(false, "There's not a method named `handleResetClick()` that's being called on anchor click in the QuizEnd class.")
26+
}
27+
28+
let mockedPropHandler = sinon.spy()
29+
30+
let quizEnd
31+
try {
32+
quizEnd = shallow(<QuizEnd resetClickHandler={mockedPropHandler} />)
33+
} catch (e) {
34+
assert(false, "We weren't able to mount the QuizEnd component")
35+
}
36+
37+
quizEnd.find('a').simulate('click')
38+
assert(spy.calledOnce, "There's not a method named `handleResetClick()` that's being called on the anchor click in the QuizEnd class.")
39+
40+
})
41+
})
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import App from '../../App';
4+
import { shallow, mount } from 'enzyme';
5+
import { assert } from 'chai';
6+
import sinon from 'sinon';
7+
8+
let quizComponentExists = false;
9+
let Quiz;
10+
try {
11+
Quiz = require('../../Quiz.js').default;
12+
quizComponentExists = true;
13+
} catch (e) {
14+
quizComponentExists = false;
15+
}
16+
17+
let quizEndComponentExists = false;
18+
let QuizEnd;
19+
try {
20+
QuizEnd = require('../../QuizEnd.js').default;
21+
quizEndComponentExists = true;
22+
} catch (e) {
23+
quizEndComponentExists = false;
24+
}
25+
26+
describe('QuizEnd Component', () => {
27+
it('handleResetClick method called resetClickHandler @quiz-end-handle-click-method-returns-correct-value', () => {
28+
assert(quizComponentExists, "The Quiz component hasn't been created yet.")
29+
assert(quizEndComponentExists, "The QuizEnd component hasn't been created yet.")
30+
31+
let quiz
32+
try {
33+
quiz = shallow(<Quiz />)
34+
} catch (e) {
35+
console.log(e)
36+
assert(false, "We weren't able to mount the Quiz component")
37+
}
38+
39+
let quiz_spy
40+
try {
41+
quiz_spy = sinon.spy(Quiz.prototype, 'handleResetClick')
42+
} catch (e) {
43+
assert(false, "There's not a method named `handleResetClick()` in the Quiz class.")
44+
}
45+
46+
let quiz_end_spy
47+
try {
48+
quiz_end_spy = sinon.spy(QuizEnd.prototype, 'handleResetClick')
49+
} catch (e) {
50+
assert(false, "There's not a method named `handleResetClick()` in the QuizEnd class.")
51+
}
52+
53+
let mockedPropHandler = sinon.spy()
54+
55+
let mock_prop = {
56+
instruction_text: "How many continents are there on Planet Earth?",
57+
answer_options: ["5", "6", "7", "8"],
58+
answer: "5"
59+
}
60+
61+
let quizEnd
62+
try {
63+
quizEnd = shallow(<QuizEnd resetClickHandler={mockedPropHandler} />)
64+
} catch (e) {
65+
assert(false, "We weren't able to mount the QuizEnd component")
66+
}
67+
68+
quizEnd.find('a').simulate('click')
69+
assert(mockedPropHandler.called, "QuizEnd's `handleResetClick()` method isn't making a call to `this.props.resetClickHandler()`.")
70+
})
71+
})
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import App from '../../App';
4+
import { shallow } from 'enzyme';
5+
import { assert } from 'chai';
6+
import sinon from 'sinon';
7+
8+
let quizExists = false;
9+
let Quiz;
10+
try {
11+
Quiz = require('../../Quiz.js').default;
12+
quizExists = true;
13+
} catch (e) {
14+
quizExists = false;
15+
}
16+
17+
let quizEndExists = false;
18+
let QuizEnd;
19+
try {
20+
QuizEnd = require('../../QuizEnd.js').default;
21+
quizEndExists = true;
22+
} catch (e) {
23+
quizEndExists = false;
24+
}
25+
26+
let fs = require('fs');
27+
let quiz_data = require('../../quiz_data.json')
28+
let babylon = require('babylon')
29+
30+
describe('QuizEnd Component', () => {
31+
it('has a method named `handleResetClick` and a renders a QuizEnd component with a `resetClickHandler` prop @quiz-end-has-reset-click-handler', () => {
32+
assert(quizExists, "The Quiz component hasn't been created yet.")
33+
assert(quizEndExists, "The QuizEnd component hasn't been created yet.")
34+
35+
let quiz;
36+
37+
try {
38+
quiz = shallow(<Quiz />)
39+
} catch (e) {
40+
assert(false, "We weren't able to mount the Quiz component")
41+
}
42+
43+
quiz.setState({ quiz_position: quiz_data.quiz_questions.length + 1 })
44+
45+
assert(quiz.instance().handleResetClick, "There doesn't appear to be a method named `handleResetClick()` in the Quiz component.")
46+
47+
assert(quiz.find('QuizEnd').props().resetClickHandler != null, "The QuizEnd tag in Quiz's JSX doesn't have a `resetClickHandler` property set to the correct value.")
48+
49+
assert(quiz.find('QuizEnd').props().resetClickHandler.name == 'bound handleResetClick', "The QuizEnd tag in Quiz's JSX has a `resetClickHandler` property, but the value isn't set to `this.handleResetClick.bind(this)`.")
50+
51+
})
52+
})
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import App from '../../App';
4+
import { shallow } from 'enzyme';
5+
import { assert } from 'chai';
6+
import sinon from 'sinon';
7+
8+
let quizComponentExists = false;
9+
let Quiz;
10+
try {
11+
Quiz = require('../../Quiz.js').default;
12+
quizComponentExists = true;
13+
} catch (e) {
14+
quizComponentExists = false;
15+
}
16+
17+
let fs = require('fs');
18+
let quiz_data = require('../../quiz_data.json')
19+
let babylon = require('babylon')
20+
21+
describe('Quiz Component', () => {
22+
it('handleResetClick() method sets the quiz_position to 1 @quiz-handle-reset-click-sets-position-state', () => {
23+
assert(quizComponentExists, "The Quiz component hasn't been created yet.")
24+
25+
let quiz;
26+
27+
try {
28+
quiz = shallow(<Quiz />)
29+
} catch (e) {
30+
assert(false, "We weren't able to mount the Quiz component")
31+
}
32+
33+
quiz.setState({ quiz_position: 3 })
34+
35+
assert(quiz.state().quiz_position == 3, "The Quiz component's state does not have a key named `quiz_position` with the correct value - are you sure you're still setting the component's state to `quiz_data`?")
36+
quiz.instance().handleResetClick()
37+
38+
assert(quiz.state().quiz_position == 1, "The Quiz component state's `quiz_position` value is not being reset to 1 when the `handleResetClick()` method is called.")
39+
})
40+
})

0 commit comments

Comments
 (0)