Skip to content

Commit 5f19a1f

Browse files
committed
test: use initial db
1 parent 8d2097d commit 5f19a1f

40 files changed

+703
-606
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ dist
33
coverage
44
src/registerServiceWorker.js
55
cypress/integration/examples
6+
test/tmp

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@
6767
"report": "cat ./coverage/lcov.info | env-cmd -f ./.env.test codacy-coverage",
6868
"report:ci": "cat ./coverage/lcov.info | codacy-coverage",
6969
"version": "git add CHANGELOG.md && standard-version -a",
70-
"mocha": "mkdirp test/tmp && concurrently \"env-cmd -f ./.env.test react-scripts start\" \"wait-on http://localhost:3000 && mocha --require @babel/register \"test/**/*.test.js\"\""
70+
"mocha:run": "mocha --require @babel/register --retries 3 'test/**/*.test.js'",
71+
"mocha": "mkdirp test/tmp && concurrently \"env-cmd -f ./.env.test react-scripts start\" \"wait-on http://localhost:3000 && yarn mocha:run\""
7172
},
7273
"dependencies": {
7374
"@material-ui/core": "4.11.0",

public/app/listeners/loadSpace.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,23 @@ const loadSpace = (mainWindow, db) => async (
172172
return mainWindow.webContents.send(LOADED_SPACE_CHANNEL, ERROR_GENERAL);
173173
}
174174

175-
const savedResources = db.get(APP_INSTANCE_RESOURCES_COLLECTION);
175+
// remove already existing resources
176+
db.get(APP_INSTANCE_RESOURCES_COLLECTION)
177+
.remove(({ id }) =>
178+
appInstanceResources.find(({ id: thisId }) => thisId === id)
179+
)
180+
.write();
176181

177182
const newResources = appInstanceResources
178-
// keep only non-duplicate resources
179-
.filter(({ id }) => !savedResources.find({ id }).value())
180183
// change user id by current user id
181184
.map((resource) => ({
182185
...resource,
183186
user: userId,
184187
}));
185188

186-
savedResources.push(...newResources).write();
189+
db.get(APP_INSTANCE_RESOURCES_COLLECTION)
190+
.push(...newResources)
191+
.write();
187192
}
188193

189194
// write actions to database if selected

src/components/common/MainMenu.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
SIGN_OUT_MENU_ITEM_ID,
4545
SAVED_SPACES_MENU_ITEM_ID,
4646
CLASSROOMS_MENU_ITEM_ID,
47+
MAINMENU_ID,
4748
} from '../../config/selectors';
4849
import { signOut } from '../../actions/authentication';
4950
import { AUTHENTICATED, USER_MODES } from '../../config/constants';
@@ -66,7 +67,7 @@ export class MainMenu extends Component {
6667
user: Map(),
6768
};
6869

69-
handleClick = path => {
70+
handleClick = (path) => {
7071
const {
7172
history: { push },
7273
} = this.props;
@@ -145,7 +146,7 @@ export class MainMenu extends Component {
145146
return null;
146147
}
147148

148-
renderOfflineMenuItem = child => {
149+
renderOfflineMenuItem = (child) => {
149150
const { t } = this.props;
150151

151152
return (
@@ -325,7 +326,7 @@ export class MainMenu extends Component {
325326
t,
326327
} = this.props;
327328
return (
328-
<List>
329+
<List id={MAINMENU_ID}>
329330
{this.renderAuthenticatedMenu()}
330331
<MenuItem
331332
id={SETTINGS_MENU_ITEM_ID}

src/components/common/__snapshots__/MainMenu.test.js.snap

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`<MainMenu /> <MainMenu /> with developerMode = false renders correctly 1`] = `
4-
<WithStyles(ForwardRef(List))>
4+
<WithStyles(ForwardRef(List))
5+
id="mainMenu"
6+
>
57
<WithStyles(ForwardRef(MenuItem))
68
button={true}
79
id="homeMenuItem"
@@ -183,7 +185,9 @@ exports[`<MainMenu /> <MainMenu /> with developerMode = false renders correctly
183185
`;
184186

185187
exports[`<MainMenu /> <MainMenu /> with developerMode = true renders correctly 1`] = `
186-
<WithStyles(ForwardRef(List))>
188+
<WithStyles(ForwardRef(List))
189+
id="mainMenu"
190+
>
187191
<WithStyles(ForwardRef(MenuItem))
188192
button={true}
189193
id="homeMenuItem"

src/components/space/export/ExportSelectionScreen.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ import {
3232
ERROR_MESSAGE_HEADER,
3333
UNEXPECTED_ERROR_MESSAGE,
3434
} from '../../../config/messages';
35+
import { USER_MODES } from '../../../config/constants';
3536

36-
const styles = theme => ({
37+
const styles = (theme) => ({
3738
...Styles(theme),
3839
buttonGroup: {
3940
textAlign: 'center',
@@ -95,6 +96,7 @@ class ExportSelectionScreen extends Component {
9596
}).isRequired,
9697
space: PropTypes.instanceOf(Map).isRequired,
9798
status: PropTypes.oneOf(Object.values(EXPORT_SPACE_STATUS)).isRequired,
99+
isTeacher: PropTypes.bool.isRequired,
98100
};
99101

100102
state = {
@@ -137,7 +139,7 @@ class ExportSelectionScreen extends Component {
137139
}
138140
};
139141

140-
handleChange = event => {
142+
handleChange = (event) => {
141143
this.setState({ [event.target.name]: event.target.checked });
142144
};
143145

@@ -168,12 +170,12 @@ class ExportSelectionScreen extends Component {
168170
};
169171

170172
renderCheckbox(collectionName, label, isChecked, emptyHelperText) {
171-
const { database, space, userId } = this.props;
173+
const { database, space, userId, isTeacher } = this.props;
172174

173175
const id = space.get('id');
174176
const content = database ? database[collectionName] : [];
175177
const hasContent = content.filter(
176-
({ user, spaceId }) => user === userId && spaceId === id
178+
({ user, spaceId }) => (isTeacher || user === userId) && spaceId === id
177179
).length;
178180

179181
const checkbox = (
@@ -313,6 +315,9 @@ const mapStateToProps = ({
313315
Developer,
314316
}) => ({
315317
userId: authentication.getIn(['user', 'id']),
318+
isTeacher:
319+
authentication.getIn(['user', 'settings', 'userMode']) ===
320+
USER_MODES.TEACHER,
316321
database: Developer.get('database'),
317322
activity: Boolean(exportSpaceReducer.getIn(['activity']).size),
318323
space: exportSpaceReducer.getIn(['space']),

src/config/selectors.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export const DRAWER_BUTTON_ID = 'drawerButton';
33
export const FAVORITE_SPACES_WRAPPER_ID = 'favoriteSpacesWrapper';
44
export const RECENT_SPACES_WRAPPER_ID = 'recentSpacesWrapper';
55

6+
export const MAINMENU_ID = 'mainMenu';
67
export const CLASSROOMS_MENU_ITEM_ID = 'classroomsMenuItem';
78
export const CLASSROOMS_MAIN_ID = 'classroomsMain';
89
export const SAVED_SPACES_MENU_ITEM_ID = 'savedSpacesMenuItem';

test/application.js

+76-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,83 @@
11
import { Application } from 'spectron';
22
import electronPath from 'electron'; // Require Electron from the binaries included in node_modules.
33
import path from 'path';
4+
import fse from 'fs-extra';
5+
import extract from 'extract-zip';
6+
import { buildSignedUserForDatabase } from './constants';
47

5-
const createApplication = async (
6-
{
7-
showMessageDialogResponse,
8-
showSaveDialogResponse,
9-
showOpenDialogResponse,
10-
showTours,
11-
} = {
8+
const getFormattedTime = () => {
9+
const today = new Date();
10+
const y = today.getFullYear();
11+
// JavaScript months are 0-based.
12+
const m = today.getMonth() + 1;
13+
const d = today.getDate();
14+
const h = today.getHours();
15+
const mi = today.getMinutes();
16+
const s = today.getSeconds();
17+
return `${y}${m}${d}_${h}-${mi}-${s}`;
18+
};
19+
20+
const setUpDatabase = async (database = buildSignedUserForDatabase()) => {
21+
const tmpDatabasePath = path.join(__dirname, 'tmp', getFormattedTime());
22+
const varFolder = path.join(tmpDatabasePath, 'var');
23+
fse.ensureDirSync(varFolder);
24+
25+
const db = {
26+
...database,
27+
spaces: [],
28+
appInstanceResources: [],
29+
actions: [],
30+
};
31+
32+
if (database) {
33+
// add paths data in var
34+
const spaces = database?.spaces || [];
35+
// eslint-disable-next-line no-restricted-syntax
36+
for (const {
37+
path: spacePath,
38+
space,
39+
appInstanceResources,
40+
actions,
41+
} of spaces) {
42+
// eslint-disable-next-line no-await-in-loop
43+
await extract(path.join(__dirname, './fixtures/spaces', spacePath), {
44+
dir: `${varFolder}/${space.id}`,
45+
});
46+
db.spaces.push(space);
47+
db.appInstanceResources = db.appInstanceResources.concat(
48+
appInstanceResources
49+
);
50+
db.actions = db.actions.concat(actions);
51+
}
52+
53+
const classrooms = database?.classrooms || [];
54+
// eslint-disable-next-line no-restricted-syntax
55+
for (const { id } of classrooms) {
56+
fse.ensureDirSync(path.join(varFolder, id));
57+
}
58+
59+
// set db
60+
fse.writeFileSync(`${varFolder}/db.json`, JSON.stringify(db));
61+
}
62+
63+
return tmpDatabasePath;
64+
};
65+
66+
const createApplication = async ({
67+
database = buildSignedUserForDatabase(),
68+
responses = {
1269
showMessageDialogResponse: undefined,
1370
showSaveDialogResponse: undefined,
1471
showOpenDialogResponse: undefined,
1572
showTours: 0,
16-
}
17-
) => {
73+
},
74+
} = {}) => {
75+
const {
76+
showMessageDialogResponse,
77+
showSaveDialogResponse,
78+
showOpenDialogResponse,
79+
showTours,
80+
} = responses;
1881
const env = { NODE_ENV: 'test', ELECTRON_IS_DEV: 0, SHOW_TOURS: showTours };
1982

2083
if (showMessageDialogResponse !== undefined) {
@@ -29,6 +92,9 @@ const createApplication = async (
2992
env.SHOW_OPEN_DIALOG_RESPONSE = showOpenDialogResponse;
3093
}
3194

95+
// set up database
96+
const tmpDatabasePath = await setUpDatabase(database);
97+
3298
const app = new Application({
3399
// Your electron path can be any binary
34100
// i.e for OSX an example path could be '/Applications/MyApp.app/Contents/MacOS/MyApp'
@@ -49,6 +115,7 @@ const createApplication = async (
49115
// The following line tells spectron to look and use the main.js file
50116
// and the package.json located 1 level above.
51117
args: [path.join(__dirname, '../public/electron.js')],
118+
chromeDriverArgs: [`--user-data-dir=${tmpDatabasePath}`],
52119
env,
53120
});
54121

0 commit comments

Comments
 (0)