Skip to content

Commit

Permalink
test: use initial db
Browse files Browse the repository at this point in the history
  • Loading branch information
pyphilia committed Nov 24, 2020
1 parent 8d2097d commit 5f19a1f
Show file tree
Hide file tree
Showing 40 changed files with 703 additions and 606 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ dist
coverage
src/registerServiceWorker.js
cypress/integration/examples
test/tmp
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
"report": "cat ./coverage/lcov.info | env-cmd -f ./.env.test codacy-coverage",
"report:ci": "cat ./coverage/lcov.info | codacy-coverage",
"version": "git add CHANGELOG.md && standard-version -a",
"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\"\""
"mocha:run": "mocha --require @babel/register --retries 3 'test/**/*.test.js'",
"mocha": "mkdirp test/tmp && concurrently \"env-cmd -f ./.env.test react-scripts start\" \"wait-on http://localhost:3000 && yarn mocha:run\""
},
"dependencies": {
"@material-ui/core": "4.11.0",
Expand Down
13 changes: 9 additions & 4 deletions public/app/listeners/loadSpace.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,23 @@ const loadSpace = (mainWindow, db) => async (
return mainWindow.webContents.send(LOADED_SPACE_CHANNEL, ERROR_GENERAL);
}

const savedResources = db.get(APP_INSTANCE_RESOURCES_COLLECTION);
// remove already existing resources
db.get(APP_INSTANCE_RESOURCES_COLLECTION)
.remove(({ id }) =>
appInstanceResources.find(({ id: thisId }) => thisId === id)
)
.write();

const newResources = appInstanceResources
// keep only non-duplicate resources
.filter(({ id }) => !savedResources.find({ id }).value())
// change user id by current user id
.map((resource) => ({
...resource,
user: userId,
}));

savedResources.push(...newResources).write();
db.get(APP_INSTANCE_RESOURCES_COLLECTION)
.push(...newResources)
.write();
}

// write actions to database if selected
Expand Down
7 changes: 4 additions & 3 deletions src/components/common/MainMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
SIGN_OUT_MENU_ITEM_ID,
SAVED_SPACES_MENU_ITEM_ID,
CLASSROOMS_MENU_ITEM_ID,
MAINMENU_ID,
} from '../../config/selectors';
import { signOut } from '../../actions/authentication';
import { AUTHENTICATED, USER_MODES } from '../../config/constants';
Expand All @@ -66,7 +67,7 @@ export class MainMenu extends Component {
user: Map(),
};

handleClick = path => {
handleClick = (path) => {
const {
history: { push },
} = this.props;
Expand Down Expand Up @@ -145,7 +146,7 @@ export class MainMenu extends Component {
return null;
}

renderOfflineMenuItem = child => {
renderOfflineMenuItem = (child) => {
const { t } = this.props;

return (
Expand Down Expand Up @@ -325,7 +326,7 @@ export class MainMenu extends Component {
t,
} = this.props;
return (
<List>
<List id={MAINMENU_ID}>
{this.renderAuthenticatedMenu()}
<MenuItem
id={SETTINGS_MENU_ITEM_ID}
Expand Down
8 changes: 6 additions & 2 deletions src/components/common/__snapshots__/MainMenu.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`<MainMenu /> <MainMenu /> with developerMode = false renders correctly 1`] = `
<WithStyles(ForwardRef(List))>
<WithStyles(ForwardRef(List))
id="mainMenu"
>
<WithStyles(ForwardRef(MenuItem))
button={true}
id="homeMenuItem"
Expand Down Expand Up @@ -183,7 +185,9 @@ exports[`<MainMenu /> <MainMenu /> with developerMode = false renders correctly
`;

exports[`<MainMenu /> <MainMenu /> with developerMode = true renders correctly 1`] = `
<WithStyles(ForwardRef(List))>
<WithStyles(ForwardRef(List))
id="mainMenu"
>
<WithStyles(ForwardRef(MenuItem))
button={true}
id="homeMenuItem"
Expand Down
13 changes: 9 additions & 4 deletions src/components/space/export/ExportSelectionScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ import {
ERROR_MESSAGE_HEADER,
UNEXPECTED_ERROR_MESSAGE,
} from '../../../config/messages';
import { USER_MODES } from '../../../config/constants';

const styles = theme => ({
const styles = (theme) => ({
...Styles(theme),
buttonGroup: {
textAlign: 'center',
Expand Down Expand Up @@ -95,6 +96,7 @@ class ExportSelectionScreen extends Component {
}).isRequired,
space: PropTypes.instanceOf(Map).isRequired,
status: PropTypes.oneOf(Object.values(EXPORT_SPACE_STATUS)).isRequired,
isTeacher: PropTypes.bool.isRequired,
};

state = {
Expand Down Expand Up @@ -137,7 +139,7 @@ class ExportSelectionScreen extends Component {
}
};

handleChange = event => {
handleChange = (event) => {
this.setState({ [event.target.name]: event.target.checked });
};

Expand Down Expand Up @@ -168,12 +170,12 @@ class ExportSelectionScreen extends Component {
};

renderCheckbox(collectionName, label, isChecked, emptyHelperText) {
const { database, space, userId } = this.props;
const { database, space, userId, isTeacher } = this.props;

const id = space.get('id');
const content = database ? database[collectionName] : [];
const hasContent = content.filter(
({ user, spaceId }) => user === userId && spaceId === id
({ user, spaceId }) => (isTeacher || user === userId) && spaceId === id
).length;

const checkbox = (
Expand Down Expand Up @@ -313,6 +315,9 @@ const mapStateToProps = ({
Developer,
}) => ({
userId: authentication.getIn(['user', 'id']),
isTeacher:
authentication.getIn(['user', 'settings', 'userMode']) ===
USER_MODES.TEACHER,
database: Developer.get('database'),
activity: Boolean(exportSpaceReducer.getIn(['activity']).size),
space: exportSpaceReducer.getIn(['space']),
Expand Down
1 change: 1 addition & 0 deletions src/config/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const DRAWER_BUTTON_ID = 'drawerButton';
export const FAVORITE_SPACES_WRAPPER_ID = 'favoriteSpacesWrapper';
export const RECENT_SPACES_WRAPPER_ID = 'recentSpacesWrapper';

export const MAINMENU_ID = 'mainMenu';
export const CLASSROOMS_MENU_ITEM_ID = 'classroomsMenuItem';
export const CLASSROOMS_MAIN_ID = 'classroomsMain';
export const SAVED_SPACES_MENU_ITEM_ID = 'savedSpacesMenuItem';
Expand Down
85 changes: 76 additions & 9 deletions test/application.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,83 @@
import { Application } from 'spectron';
import electronPath from 'electron'; // Require Electron from the binaries included in node_modules.
import path from 'path';
import fse from 'fs-extra';
import extract from 'extract-zip';
import { buildSignedUserForDatabase } from './constants';

const createApplication = async (
{
showMessageDialogResponse,
showSaveDialogResponse,
showOpenDialogResponse,
showTours,
} = {
const getFormattedTime = () => {
const today = new Date();
const y = today.getFullYear();
// JavaScript months are 0-based.
const m = today.getMonth() + 1;
const d = today.getDate();
const h = today.getHours();
const mi = today.getMinutes();
const s = today.getSeconds();
return `${y}${m}${d}_${h}-${mi}-${s}`;
};

const setUpDatabase = async (database = buildSignedUserForDatabase()) => {
const tmpDatabasePath = path.join(__dirname, 'tmp', getFormattedTime());
const varFolder = path.join(tmpDatabasePath, 'var');
fse.ensureDirSync(varFolder);

const db = {
...database,
spaces: [],
appInstanceResources: [],
actions: [],
};

if (database) {
// add paths data in var
const spaces = database?.spaces || [];
// eslint-disable-next-line no-restricted-syntax
for (const {
path: spacePath,
space,
appInstanceResources,
actions,
} of spaces) {
// eslint-disable-next-line no-await-in-loop
await extract(path.join(__dirname, './fixtures/spaces', spacePath), {
dir: `${varFolder}/${space.id}`,
});
db.spaces.push(space);
db.appInstanceResources = db.appInstanceResources.concat(
appInstanceResources
);
db.actions = db.actions.concat(actions);
}

const classrooms = database?.classrooms || [];
// eslint-disable-next-line no-restricted-syntax
for (const { id } of classrooms) {
fse.ensureDirSync(path.join(varFolder, id));
}

// set db
fse.writeFileSync(`${varFolder}/db.json`, JSON.stringify(db));
}

return tmpDatabasePath;
};

const createApplication = async ({
database = buildSignedUserForDatabase(),
responses = {
showMessageDialogResponse: undefined,
showSaveDialogResponse: undefined,
showOpenDialogResponse: undefined,
showTours: 0,
}
) => {
},
} = {}) => {
const {
showMessageDialogResponse,
showSaveDialogResponse,
showOpenDialogResponse,
showTours,
} = responses;
const env = { NODE_ENV: 'test', ELECTRON_IS_DEV: 0, SHOW_TOURS: showTours };

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

// set up database
const tmpDatabasePath = await setUpDatabase(database);

const app = new Application({
// Your electron path can be any binary
// i.e for OSX an example path could be '/Applications/MyApp.app/Contents/MacOS/MyApp'
Expand All @@ -49,6 +115,7 @@ const createApplication = async (
// The following line tells spectron to look and use the main.js file
// and the package.json located 1 level above.
args: [path.join(__dirname, '../public/electron.js')],
chromeDriverArgs: [`--user-data-dir=${tmpDatabasePath}`],
env,
});

Expand Down
Loading

0 comments on commit 5f19a1f

Please sign in to comment.