Skip to content

Commit 0d58972

Browse files
authored
Merge pull request #4 from oslabs-beta/robby/testing
Robby/testing - add new test file for backend RoutesDescription, update test cases to reflect changes in backend hierarchy
2 parents 61bd748 + c0ce871 commit 0d58972

File tree

7 files changed

+1172
-1160
lines changed

7 files changed

+1172
-1160
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"build": "webpack --mode production",
1717
"dev": "webpack --mode development --watch",
1818
"test": "jest --verbose --coverage",
19+
"test-on": "./node_modules/.bin/jest $1",
1920
"docker-test-lint": "eslint --ext .js --ext .jsx src",
2021
"docs": "typedoc --json docs --inputFiles src/app --inputFiles src/backend --readme docs/readme.md"
2122
},
@@ -113,7 +114,7 @@
113114
"jest-diff": "^26.1.0",
114115
"jest-runner-eslint": "^0.7.7",
115116
"jscharting-react": "^1.2.1",
116-
"puppeteer": "^5.1.0",
117+
"puppeteer": "^14.3.0",
117118
"sass": "^1.26.10",
118119
"sass-loader": "^7.3.1",
119120
"sinon-chrome": "^3.0.1",

src/app/__tests__/ActionContainer.test.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import ActionContainer from '../containers/ActionContainer';
88
import { useStoreContext } from '../store';
99
import { emptySnapshots } from '../actions/actions';
1010
import Action from '../components/Action';
11+
import RouteDescription from '../components/RouteDescription';
1112

1213
configure({ adapter: new (Adapter as any)() });
1314

@@ -26,6 +27,10 @@ const state = {
2627
name: 'App',
2728
componentData: { actualDuration: 3.5 },
2829
}],
30+
route: {
31+
id: 1,
32+
url: 'http://localhost:8080/',
33+
},
2934
},
3035
children: [{
3136
index: 1,
@@ -38,6 +43,10 @@ const state = {
3843
name: 'App',
3944
componentData: { actualDuration: 3.5 },
4045
}],
46+
route: {
47+
id: 2,
48+
url: 'http://localhost:8080/',
49+
},
4150
},
4251
children: [{
4352
index: 2,
@@ -50,6 +59,10 @@ const state = {
5059
name: 'App',
5160
componentData: { actualDuration: 3.5 },
5261
}],
62+
route: {
63+
id: 3,
64+
url: 'http://localhost:8080/',
65+
},
5366
},
5467
children: [{
5568
index: 3,
@@ -62,6 +75,10 @@ const state = {
6275
name: 'App',
6376
componentData: { actualDuration: 3.5 },
6477
}],
78+
route: {
79+
id: 4,
80+
url: 'http://localhost:8080/test/',
81+
},
6582
},
6683
children: [],
6784
}],
@@ -87,11 +104,12 @@ useStoreContext.mockImplementation(() => [state, dispatch]);
87104

88105
let wrapper;
89106

90-
//actionView={true} must be passed in to <ActionContainer /> in beforeEach() to deal with new
91-
//conditional rendering in ActionContainer that shows/hides time-travel functionality
107+
// actionView={true} must be passed in to <ActionContainer /> in beforeEach() to deal with new
108+
// conditional rendering in ActionContainer that shows/hides time-travel functionality
92109

93110
beforeEach(() => {
94111
wrapper = shallow(<ActionContainer actionView={true} />);
112+
// wrapper2 = shallow(<RouteDescription />);
95113
useStoreContext.mockClear();
96114
dispatch.mockClear();
97115
});
@@ -107,6 +125,6 @@ describe('testing the emptySnapshot button', () => {
107125
});
108126
});
109127

110-
test('number of actions should reflect snapshots array', () => {
111-
expect(wrapper.find(Action).length).toBe(state.tabs[state.currentTab].snapshots.length);
128+
test('number of RouteDescription components should reflect number of unique routes', () => {
129+
expect(wrapper.find(RouteDescription).length).toBe(2);
112130
});

src/backend/__tests__/linkFiber.test.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import puppeteer from 'puppeteer';
1515

1616
const SERVER = require('../puppeteerServer');
1717

18-
const APP = 'http://localhost:5000';
18+
// Apple uses port 5000 for Air Play.
19+
const APP = 'http://localhost:5001';
1920

2021
let linkFiber;
2122
let mode;
Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,61 @@
1-
import masterState from '../masterState'
1+
import masterState from '../masterState';
22
import {
33
// eslint-disable-next-line @typescript-eslint/no-unused-vars
44
HookStateItem,
5-
HookStates
5+
HookStates,
66
} from '../types/backendTypes';
77

88
describe('Testing masterState functionality', () => {
9-
let hookOne : HookStateItem = {state: 'A', component: 'counter1'};
10-
let hookTwo : HookStateItem = {state: 'B', component: 'counter2'};
11-
let allHooks : HookStates = [hookOne, hookTwo];
12-
masterState.saveNew(hookOne.state, hookOne.component);
13-
masterState.saveNew(hookTwo.state, hookTwo.component);
9+
const hookOne : HookStateItem = { state: 'A', component: 'counter1' };
10+
const hookTwo : HookStateItem = { state: 'B', component: 'counter2' };
11+
const hookThree : HookStateItem = { state: 'C', component: 'counter3' };
12+
const allHooks : HookStates = [hookOne, hookTwo];
1413

15-
describe('Save new', () => {
16-
masterState.saveNew(hookOne.state, hookOne.component);
17-
masterState.saveNew(hookTwo.state, hookTwo.component);
18-
})
14+
describe('saveNew', () => {
15+
it('Should return the index of the saved component', () => {
16+
expect(masterState.saveNew(hookOne.state, hookOne.component)).toBe(0);
17+
expect(masterState.saveNew(hookTwo.state, hookTwo.component)).toBe(1);
18+
});
19+
});
1920

2021
describe('getComponentByIndex', () => {
21-
it('should be able to get both hook states component', () => {
22+
it('Should return the component when given a valid index', () => {
2223
expect(masterState.getComponentByIndex(0)).toEqual(hookOne.component);
2324
expect(masterState.getComponentByIndex(1)).toEqual(hookTwo.component);
24-
})
25-
})
25+
});
26+
it('Should return undefined when given an invalid index', () => {
27+
expect(masterState.getComponentByIndex(2)).toBe(undefined);
28+
});
29+
});
2630

2731
describe('getRecordByIndex', () => {
28-
it('should be able to get both hook states', () => {
32+
it('Should return the record when given a valid index', () => {
2933
expect(masterState.getRecordByIndex(0)).toEqual(hookOne);
3034
expect(masterState.getRecordByIndex(1)).toEqual(hookTwo);
31-
})
32-
})
35+
});
36+
it('Should return undefined when given an invalid index', () => {
37+
expect(masterState.getRecordByIndex(2)).toBe(undefined);
38+
});
39+
});
3340

34-
35-
})
41+
describe('getComponentByIndexHooks', () => {
42+
it('Should return an array of components when given an a valid array of indices', () => {
43+
expect(masterState.getComponentByIndexHooks([0, 1])).toEqual([hookOne.component, hookTwo.component]);
44+
});
45+
it('Should return an empty array when given an invalid array of indices', () => {
46+
expect(masterState.getComponentByIndexHooks([2])).toEqual([]);
47+
});
48+
});
49+
50+
describe('clear', () => {
51+
it('Should return undefined', () => {
52+
expect(masterState.clear()).toBe(undefined);
53+
});
54+
it('Should reset the componentActionRecord index', () => {
55+
expect(masterState.saveNew(hookThree.state, hookThree.component)).toBe(0);
56+
});
57+
it('Should empty the componentActionRecord array', () => {
58+
expect(masterState.getComponentByIndex(0)).toEqual(hookThree.component);
59+
});
60+
});
61+
});

src/backend/__tests__/routes.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import routes from '../routes';
2+
3+
describe('Unit Tests for routes.ts', () => {
4+
describe('addRoute', () => {
5+
it('If given a url that doesn\'t match the current route, it should return a new route object and add the route object to its values array.', () => {
6+
const newRoute = routes.addRoute('http://localhost:8080/');
7+
expect(newRoute.url).toBe('http://localhost:8080/');
8+
expect(newRoute.id).toBe(1);
9+
expect(routes.values[1].url).toBe('http://localhost:8080/');
10+
expect(routes.values[1].id).toBe(1);
11+
});
12+
13+
it('If given a url that does match the current route, it should return the current route object and not add anything to its values array.', () => {
14+
const sameRoute = routes.addRoute('http://localhost:8080/');
15+
expect(sameRoute).toBe(routes.values[1]);
16+
expect(routes.values.length).toBe(2);
17+
});
18+
19+
it('Should give route objects unique ids.', () => {
20+
routes.addRoute('http://localhost:8080/test');
21+
expect(routes.values[1].id).not.toBe(routes.values[2].id);
22+
});
23+
24+
it('Should reassign current to point to the last index in the values array.', () => {
25+
expect(routes.current).toBe(routes.values.length - 1);
26+
});
27+
});
28+
29+
describe('navigate', () => {
30+
it('Should correctly calculate delta between current and target route.', () => {
31+
routes.addRoute('http://localhost:8080/test1');
32+
routes.navigate({ url: 'http://localhost:8080/', id: 1 });
33+
expect(routes.current).toBe(1);
34+
routes.navigate({ url: 'http://localhost:8080/test1', id: 3 });
35+
expect(routes.current).toBe(3);
36+
});
37+
38+
it('Should return true if it navigated.', () => {
39+
expect(routes.navigate({ url: 'http://localhost:8080/', id: 1 })).toBe(true);
40+
});
41+
42+
it('Should return false if it didn\'t navigate.', () => {
43+
expect(routes.navigate({ url: 'http://localhost:8080/', id: 1 })).toBe(false);
44+
});
45+
});
46+
});

src/backend/puppeteerServer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const app = express();
88

99
app.use(express.static(path.resolve(__dirname)));
1010

11-
const server = app.listen(5000);
11+
// Apple uses port 5000 for Air Play.
12+
const server = app.listen(5001);
1213

1314
module.exports = server;

0 commit comments

Comments
 (0)