11/* eslint-disable */
2+ /* All Challenge Test Runner */
23
34import assert from 'assert'
45import { transform } from 'babel-standalone'
@@ -7,100 +8,16 @@ const shallow = Enzyme.shallow;
78const mount = Enzyme . mount ;
89const render = Enzyme . render ;
910
10- /* NOTE: About running these tests:
11- *
12- * All the challenges are imported and tested below. Uncommented the ones you do
13- * not want in the challenges[] array below to run one in isolation.
14- *
15- * Then, uncomment line 22 to see the test results for the challenge in the
16- * console.
17- *
18- * The boolean in line 21 (true) is there to turn error suppression on
19- * for some of the default logging we have.
20- *
21- * You can add additional logs in the test file as needed when updating the tests.
22- *
23- * Run `npm test` in terminal to get started. Have fun. ┻━┻ ︵ ლ(⌒-⌒ლ)
24- */
25-
26- // const createJestTest = ({ id }) => {
27- // return test(id, () => {
28- // const solutionCode = eval(id).solutionCode
29- // const executeTests = eval(id).executeTests;
30- // const { passed, testResults } = executeTests(solutionCode, true);
31- // // console.log(testResults);
32- // expect(passed).toBe(true);
33- // });
34- // }
35-
36- // if (title.includes('Optimize')) {
37- // try {
38- // (() => {
39- // const result = eval(
40- // head + '\n;;' +
41- // solution + '\n;;' +
42- // tail + '\n;;' +
43- // test);
44-
45- // if (typeof result === 'object' && result.then) {
46- // return Observable.fromPromise(result);
47- // }
48-
49- // })();
50- // } catch (e) {
51- // console.log('Error:');
52- // console.log(e);
53- // }
54- // }
55-
56- "class OnlyEvens extends React.Component {\n constructor(props) {\n super(props);\n }\n shouldComponentUpdate(nextProps, nextState) {\n console.log('Should I update?');\n // change code below this line\n return nextProps.value % 2 === 0;\n // change code above this line\n }\n componentWillReceiveProps(nextProps) {\n console.log('Receiving new props...');\n }\n componentDidUpdate() {\n console.log('Component re-rendered.');\n }\n render() {\n return <h1>{this.props.value}</h1>\n }\n};\n\nclass Controller extends React.Component {\n constructor(props) {\n super(props);\n this.state = {\n value: 0\n };\n this.addValue = this.addValue.bind(this); \n }\n addValue() {\n this.setState({\n value: this.state.value + 1\n });\n }\n render() {\n return (\n <div>\n <button onClick={this.addValue}>Add</button>\n <OnlyEvens value={this.state.value}/>\n </div>\n );\n }\n};"
57-
58- "const waitForIt = (fn) => new Promise((resolve, reject) => setTimeout(() => resolve(fn()), 250));"
59-
60- "(async function() { const mockedComponent = Enzyme.mount(React.createElement(Controller)); const first = () => { mockedComponent.setState({ value: 1000 }); return waitForIt(() => mockedComponent.state()); }; const second = () => { mockedComponent.setState({ value: 10 }); return waitForIt(() => mockedComponent.state()); }; const firstValue = await first(); const secondValue = await second(); return (firstValue.value === 1000 && secondValue.value === 10); })()"
61-
62- const waitForIt = ( fn ) => {
63- return new Promise ( ( resolve , reject ) => {
64- return setTimeout ( ( ) => {
65- resolve ( fn ( ) ) ;
66- } , 250 ) ;
67- } ) ;
11+ const createJestTest = ( { id } ) => {
12+ return test ( id , ( ) => {
13+ const solutionCode = eval ( id ) . solutionCode
14+ const executeTests = eval ( id ) . executeTests ;
15+ const { passed, testResults } = executeTests ( solutionCode , true ) ;
16+ // console.log(testResults); Uncomment to see indivdual challenge output
17+ expect ( passed ) . toBe ( true ) ;
18+ } ) ;
6819}
6920
70- // import challenge here:
71- import { solutionCode , executeTests } from './challenges/react/React_37' ;
72-
73- test ( "Run Async Test" , async ( ) => {
74-
75- const React = require ( 'react' ) ;
76- const Redux = require ( 'redux' ) ;
77- const ReactRedux = require ( 'react-redux' ) ;
78- const ReduxThunk = require ( 'redux-thunk' ) ;
79-
80- const blockConsole = `const console = { log: () => null };` ;
81- const exportScript = '\n export default Controller'
82- const modifiedCode = blockConsole . concat ( solutionCode . concat ( exportScript ) ) ;
83-
84- const es5 = transform ( modifiedCode , { presets : [ 'es2015' , 'stage-2' , 'react' ] } ) . code ;
85- const mockedComponent = mount ( React . createElement ( eval ( es5 ) ) ) ;
86-
87- const first = ( ) => {
88- mockedComponent . setState ( { value : 1000 } ) ;
89- return waitForIt ( ( ) => mockedComponent . state ( ) ) ;
90- } ;
91-
92- const second = ( ) => {
93- mockedComponent . setState ( { value : 10 } ) ;
94- return waitForIt ( ( ) => mockedComponent . state ( ) ) ;
95- } ;
96-
97- const firstValue = await first ( ) ;
98- const secondValue = await second ( ) ;
99-
100- assert ( firstValue . value === 1000 && secondValue . value === 10 ) ;
101-
102- } ) ;
103-
10421// import React Challenges:
10522import * as React_01 from './challenges/react/React_01'
10623import * as React_02 from './challenges/react/React_02'
@@ -267,4 +184,4 @@ const challenges = [
267184 { id : 'React_Redux_08' , title : 'Connect Redux to the Messages App' } ,
268185 { id : 'React_Redux_09' , title : 'Extract Local State into Redux' } ,
269186 { id : 'React_Redux_10' , title : 'Moving Forward From Here' }
270- ] // .forEach(createJestTest); // Run tests against each challenge
187+ ] . forEach ( createJestTest ) ; // Run tests against each challenge
0 commit comments