Skip to content

Commit 3731042

Browse files
Merge pull request #53 from nicolaspita/puppetBug
Puppeteer Reducers bug
2 parents 6b15736 + 44eacd6 commit 3731042

File tree

7 files changed

+41
-46
lines changed

7 files changed

+41
-46
lines changed

src/__tests__/jest.config.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/components/ReduxTestComponent/Middleware/Middleware.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ const Middleware = ({ middleware, index }) => {
108108
</div>
109109

110110
<div id={styles.middlewareBox}>
111-
<label htmlFor='typesFile'>Query Type</label>
111+
<label htmlFor='typesFile'>Middleware Function</label>
112112
<input
113113
id='queryType'
114114
placeholder='eg. thunk'

src/components/ReduxTestComponent/Reducer/Reducer.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Draggable } from 'react-beautiful-dnd';
33
import styles from './Reducer.module.scss';
44
import { ReduxTestCaseContext } from '../../../context/reducers/reduxTestCaseReducer';
55
import { deleteReducer, updateReducer } from '../../../context/actions/reduxTestCaseActions';
6+
import { updateHooksTestStatement } from '../../../context/actions/hooksTestCaseActions';
67

78
const closeIcon = require('../../../assets/images/close.png');
89
const dragIcon = require('../../../assets/images/drag-vertical.png');

src/components/ReduxTestComponent/Reducer/Reducer.module.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@
4545
}
4646
position: relative;
4747
}
48+

src/components/useCounter.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/context/reducers/puppeteerTestCaseReducer.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createContext } from 'react';
22
import { PuppeteerTestCaseState, PuppeteerAction } from '../../utils/puppeteerTypes';
3-
//import { actionTypes } from '../actions/puppeteerTestCaseActions';
3+
44
export const PuppeteerTestCaseContext = createContext<any>(null);
55

66
export const puppeteerTestCaseState = {
@@ -21,7 +21,7 @@ export const puppeteerTestCaseState = {
2121
browserOptionId: 0,
2222
},
2323
],
24-
statementId: 0,
24+
statementId: 1,
2525
modalOpen: false,
2626
};
2727

@@ -56,10 +56,14 @@ export const puppeteerTestCaseReducer = (
5656

5757
switch (action.type) {
5858
case 'DELETE_PUPPETEER_TEST':
59-
puppeteerStatements = puppeteerStatements.filter((statement) => statement.id !== action.id);
59+
puppeteerStatements.splice(action.id, 1);
60+
puppeteerStatements.forEach((statement) => {
61+
if (statement.id > action.id) statement.id -= 1;
62+
});
6063
return {
6164
...state,
6265
puppeteerStatements,
66+
statementId: state.statementId - 1,
6367
};
6468

6569
case 'ADD_PUPPETEER_PAINT_TIMING': {
@@ -90,7 +94,7 @@ export const puppeteerTestCaseReducer = (
9094
browserOptionId: 0,
9195
},
9296
],
93-
statementId: 0,
97+
statementId: 1,
9498
};
9599

96100
case 'DELETE_BROWSER_OPTION':
@@ -171,7 +175,6 @@ export const puppeteerTestCaseReducer = (
171175
puppeteerStatements: [...action.draggableStatements],
172176
};
173177
case 'OPEN_INFO_MODAL':
174-
console.log('reducer');
175178
return {
176179
...state,
177180
modalOpen: true,

src/context/useGenerateTest.jsx

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ function useGenerateTest(test, projectFilePath) {
184184
185185
beforeEach(() => {
186186
state = ${reducer.initialState}
187-
});\n`;
187+
});
188+
\n`;
188189
}
189190

190191
// Middleware Import Statements
@@ -420,12 +421,14 @@ function useGenerateTest(test, projectFilePath) {
420421
// Middleware Filepath
421422
function createPathToMiddlewares(statement) {
422423
let filePath = null;
424+
console.log(filePath);
423425
if (statement.middlewaresFilePath) {
424426
filePath = path.relative(projectFilePath, statement.middlewaresFilePath);
425427
filePath = filePath.replace(/\\/g, '/');
426428
}
427-
if (!testFileCode.includes(`import * as middleware from`) && filePath) {
428-
testFileCode += `import * as middleware from '../${filePath}';`;
429+
430+
if (!testFileCode.includes(`import ${statement.queryType} from `)) {
431+
testFileCode += `import ${statement.queryType} from '../${filePath}';`;
429432
}
430433
}
431434

@@ -515,37 +518,45 @@ function useGenerateTest(test, projectFilePath) {
515518

516519
// Middleware Jest Test Code
517520
const addMiddleware = (middleware) => {
518-
testFileCode += `\n it('', () => {
519-
const ${middleware.queryValue} = () => {
521+
// testFileCode += `\n it('', () => {
522+
if (!testFileCode.includes(`const store`)) {
523+
testFileCode += `\n const createStore = () => {
520524
const store = {
521525
getState: jest.fn(() => ({})),
522526
dispatch: jest.fn()
523527
}
524528
const next = jest.fn()
525529
const invoke = action => ${middleware.queryType}(store)(next)(action)
526530
return { store, next, invoke }
527-
}
528-
});
531+
}
529532
\n`;
533+
}
530534

531535
if (middleware.queryValue === 'passes_non_functional_arguments') {
532-
testFileCode += `const { next, invoke } = ${middleware.queryValue}()
536+
testFileCode += `\n
537+
it('${middleware.queryValue}', () => {
538+
const { next, invoke } = createStore()
533539
const action = {type : 'TEST'}
534540
invoke(action)
535-
expect(${middleware.querySelector}).${middleware.queryVariant}(action)`;
541+
expect(${middleware.querySelector}).${middleware.queryVariant}(action)
542+
})`;
536543
} else if (middleware.queryValue === 'calls_the_function') {
537-
testFileCode += `const { invoke } = ${middleware.queryValue}()
544+
testFileCode += `\n it('${middleware.queryValue}', () => {
545+
const { invoke } = createStore()
538546
const fn = jest.fn()
539547
invoke(fn)
540-
expect(${middleware.querySelector}).${middleware.queryVariant}()`;
548+
expect(${middleware.querySelector}).${middleware.queryVariant}()
549+
})`;
541550
} else if (middleware.queryValue === 'passes_functional_arguments') {
542-
testFileCode += `const { store, invoke } = ${middleware.queryValue}()
551+
testFileCode += `\n it('${middleware.queryValue}', () => {
552+
const { store, invoke } = createStore()
543553
invoke((dispatch, getState) => {
544554
dispatch('Test Dispatch')
545555
getState()
546556
})
547557
expect(${middleware.querySelector}).${middleware.queryVariant}('Test Dispatch')
548-
expect(${middleware.querySelector}).${middleware.queryVariant}()`;
558+
expect(${middleware.querySelector}).${middleware.queryVariant}()
559+
})`;
549560
}
550561
};
551562

@@ -554,14 +565,14 @@ function useGenerateTest(test, projectFilePath) {
554565
// pass in reducer to expect. pass in initial state as 1st arg, key
555566
// if payload exists, add key/value pair to testfile code
556567
if (reducer.payloadKey) {
557-
testFileCode += `it('${reducer.itStatement}', () => {
568+
testFileCode += `\n it('${reducer.itStatement}', () => {
558569
expect(${reducer.reducerName}(state, { type: actionTypes.${reducer.reducerAction}, ${reducer.payloadKey}: ${reducer.payloadValue} })).toEqual({
559-
...state, ${reducer.expectedKey}: ${reducer.expectedValue} })})
570+
...state, ${reducer.expectedKey}: ${reducer.expectedValue} })
560571
`;
561572
} else {
562-
testFileCode += `it('${reducer.itStatement}', () => {
573+
testFileCode += `\n it('${reducer.itStatement}', () => {
563574
expect(${reducer.reducerName}(state, { type: actionTypes.${reducer.reducerAction}})).toEqual({
564-
...state, ${reducer.expectedKey}: ${reducer.expectedValue} })})
575+
...state, ${reducer.expectedKey}: ${reducer.expectedValue} })
565576
`;
566577
}
567578
}
@@ -747,6 +758,7 @@ function useGenerateTest(test, projectFilePath) {
747758
});
748759
`;
749760
};
761+
750762
switch (test) {
751763
case 'react':
752764
var reactTestCase = testState;
@@ -808,6 +820,7 @@ function useGenerateTest(test, projectFilePath) {
808820
e4x: true,
809821
}))
810822
);
823+
811824
default:
812825
return 'not a test';
813826
}

0 commit comments

Comments
 (0)