Skip to content

Commit 2f63f16

Browse files
authored
Merge pull request #16 from oslabs-beta/staging
database testing
2 parents f1afe98 + 1a86132 commit 2f63f16

File tree

6 files changed

+212
-54
lines changed

6 files changed

+212
-54
lines changed

__tests__/dbTests.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// const collectionsController = require("../src/client/controllers/collectionsController");
2+
// const historyController = require("../src/client/controllers/historyController");
3+
4+
const Dexie = require("dexie");
5+
6+
// https://stackoverflow.com/questions/47934383/indexeddb-testing-with-jest-enzyme-referenceerror-indexeddb-is-not-defined
7+
Dexie.dependencies.indexedDB = require("fake-indexeddb");
8+
Dexie.dependencies.IDBKeyRange = require("fake-indexeddb/lib/FDBKeyRange");
9+
10+
const db = new Dexie("test");
11+
12+
db.version(2).stores({
13+
history: "id, created_at",
14+
collections: "id, created_at, &name",
15+
});
16+
17+
db.version(1).stores({
18+
history: "id, created_at",
19+
});
20+
21+
// now we have db.history and db.collections
22+
23+
// for setup and teardown tasks that are asynchronous, take care to RETURN the promise
24+
describe("db test", () => {
25+
beforeEach(() => db.history.clear().catch((err) => console.log(err)));
26+
afterEach(() => db.history.clear().catch((err) => console.log(err)));
27+
describe("history tests", () => {
28+
it("can add history with id", async () => {
29+
await db.history.put({ id: 8 });
30+
const count = await db.history.count();
31+
const arr = await db.history.toArray();
32+
expect(count).toEqual(1);
33+
expect(arr[0].id).toEqual(8);
34+
});
35+
36+
it("will not add history with an empty object", async () => {
37+
db.history.put({}).catch((err) => expect(err.name).toEqual("DataError"));
38+
const count = await db.history.count();
39+
expect(count).toEqual(0);
40+
});
41+
42+
it("will not add history without an id", async () => {
43+
await db.history
44+
.put({ created_at: Date.now() })
45+
.catch((err) => expect(err.name).toEqual("DataError"));
46+
const count = await db.history.count();
47+
expect(count).toEqual(0);
48+
});
49+
});
50+
});
51+
// describe("collection tests", () => {});
52+
// });

main.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,9 @@ ipcMain.on("import-collection", (event, args) => {
407407
return;
408408
}
409409

410+
// names is the list of existing collection names in state
411+
const collectionNames = args.map((obj) => obj.name);
412+
410413
fs.readFile(filepath, "utf-8", (err, data) => {
411414
if (err) {
412415
alert("An error ocurred reading the file :", err.message);
@@ -415,6 +418,7 @@ ipcMain.on("import-collection", (event, args) => {
415418

416419
// parse data, will throw error if not parsable
417420
let parsed;
421+
// parsed.name already exists
418422
try {
419423
parsed = JSON.parse(data);
420424
} catch {
@@ -438,6 +442,13 @@ ipcMain.on("import-collection", (event, args) => {
438442
dialog.showMessageBox(null, options);
439443
return;
440444
}
445+
// duplicate collection exists already
446+
if (collectionNames.includes(parsed.name)) {
447+
options.message = "That collection already exists in the app";
448+
options.detail = "Please rename file to something else";
449+
dialog.showMessageBox(null, options);
450+
return;
451+
}
441452
}
442453

443454
// send data to chromium for state update

package-lock.json

Lines changed: 66 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"email": "swell@getswell.io",
2424
"url": "http://www.getswell.io"
2525
},
26-
"contributors": [{
26+
"contributors": [
27+
{
2728
"name": "Kyle Combs",
2829
"email": "combskyle@gmail.com",
2930
"url": "https://github.com/texpatnyc"
@@ -129,7 +130,8 @@
129130
"createDesktopShortcut": "always"
130131
},
131132
"dmg": {
132-
"contents": [{
133+
"contents": [
134+
{
133135
"x": 130,
134136
"y": 220
135137
},
@@ -255,6 +257,7 @@
255257
"eslint-plugin-jsx-a11y": "^6.3.1",
256258
"eslint-plugin-react": "^7.20.0",
257259
"eslint-plugin-react-hooks": "^4.0.4",
260+
"fake-indexeddb": "^3.0.2",
258261
"file-loader": "^2.0.0",
259262
"html-webpack-plugin": "^3.2.0",
260263
"jest": "^24.9.0",
Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,62 @@
1-
import React, { Component } from 'react';
2-
import { connect } from 'react-redux';
3-
import * as actions from '../../actions/actions';
4-
import Collection from '../display/Collection.jsx'
5-
import collectionsController from '../../controllers/collectionsController';
1+
import React, { Component } from "react";
2+
import { connect } from "react-redux";
3+
import * as actions from "../../actions/actions";
4+
import Collection from "../display/Collection.jsx";
5+
import collectionsController from "../../controllers/collectionsController";
66

7-
const mapStateToProps = store => ({
7+
const mapStateToProps = (store) => ({
88
collections: store.business.collections,
99
});
1010

11-
const mapDispatchToProps = dispatch => ({
12-
deleteFromCollection: (collection) => { dispatch(actions.deleteFromCollection(collection)) },
13-
collectionToReqRes: (reqResArray) => { dispatch(actions.collectionToReqRes(reqResArray)) },
11+
const mapDispatchToProps = (dispatch) => ({
12+
deleteFromCollection: (collection) => {
13+
dispatch(actions.deleteFromCollection(collection));
14+
},
15+
collectionToReqRes: (reqResArray) => {
16+
dispatch(actions.collectionToReqRes(reqResArray));
17+
},
1418
});
1519

1620
class CollectionsContainer extends Component {
1721
constructor(props) {
1822
super(props);
23+
this.handleClick = this.handleClick.bind(this);
1924
}
2025

21-
render() {
26+
handleClick() {
27+
// console.log("props ->", this.props.collections);
28+
collectionsController.importCollection(this.props.collections);
29+
}
2230

23-
let collectionComponents = this.props.collections.map((collection, idx) => {
24-
return <Collection
25-
content={collection} key={idx}
26-
deleteFromCollection={this.props.deleteFromCollection}
27-
collectionToReqRes={this.props.collectionToReqRes}
28-
/>
29-
})
31+
render() {
32+
const collectionComponents = this.props.collections.map(
33+
(collection, idx) => {
34+
return (
35+
<Collection
36+
content={collection}
37+
key={idx}
38+
deleteFromCollection={this.props.deleteFromCollection}
39+
collectionToReqRes={this.props.collectionToReqRes}
40+
/>
41+
);
42+
}
43+
);
3044

3145
return (
32-
<div className={'collections-container'}>
46+
<div className="collections-container">
3347
<h1>Collections</h1>
3448
<div className="collection-import-container">
35-
<button className="import-collections" onClick={collectionsController.importCollection}>Import Collection</button>
49+
<button className="import-collections" onClick={this.handleClick}>
50+
Import Collection
51+
</button>
3652
</div>
3753
{collectionComponents}
3854
</div>
39-
)
55+
);
4056
}
4157
}
4258

4359
export default connect(
4460
mapStateToProps,
45-
mapDispatchToProps,
46-
)(CollectionsContainer);
61+
mapDispatchToProps
62+
)(CollectionsContainer);
Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,48 @@
1-
import * as store from '../store';
2-
import * as actions from '../actions/actions';
3-
import db from '../db';
4-
import { ipcRenderer } from 'electron';
5-
import uuid from 'uuid/v4';
1+
import { ipcRenderer } from "electron";
2+
import uuid from "uuid/v4";
3+
import db from "../db";
4+
import * as store from "../store";
5+
import * as actions from "../actions/actions";
66

7-
ipcRenderer.on('add-collection', (event, args) => {
7+
ipcRenderer.on("add-collection", (event, args) => {
88
collectionsController.addCollectionToIndexedDb(JSON.parse(args.data));
99
collectionsController.getCollections();
1010
});
1111

1212
const collectionsController = {
13-
1413
addCollectionToIndexedDb(collection) {
15-
db.collections.put({ ...collection })
16-
.catch(err => console.log('Error in addToCollection', err));
14+
db.collections
15+
.put({ ...collection })
16+
.catch((err) => console.log("Error in addToCollection", err));
1717
},
1818

1919
deleteCollectionFromIndexedDb(id) {
20-
db.collections.delete(id)
21-
.catch(err => console.log('Error in deleteFromCollection', err));
20+
db.collections
21+
.delete(id)
22+
.catch((err) => console.log("Error in deleteFromCollection", err));
2223
},
2324

2425
getCollections() {
25-
db.table('collections')
26+
db.table("collections")
2627
.toArray()
2728
.then((collections) => {
28-
const collectionsArr = collections.sort((a, b) => b.created_at - a.created_at);
29+
const collectionsArr = collections.sort(
30+
(a, b) => b.created_at - a.created_at
31+
);
2932
store.default.dispatch(actions.getCollections(collectionsArr));
3033
})
31-
.catch(err => console.log('Error in getCollections', err));
34+
.catch((err) => console.log("Error in getCollections", err));
3235
},
3336

3437
collectionNameExists(obj) {
3538
const { name } = obj;
36-
return new Promise((resolve, reject) => { // resolve and reject are functions!
37-
db.collections.where('name').equalsIgnoreCase(name).first(foundCollection => !!foundCollection)
38-
.then(found => resolve(found))
39+
return new Promise((resolve, reject) => {
40+
// resolve and reject are functions!
41+
db.collections
42+
.where("name")
43+
.equalsIgnoreCase(name)
44+
.first((foundCollection) => !!foundCollection)
45+
.then((found) => resolve(found))
3946
.catch((error) => {
4047
console.error(error.stack || error);
4148
reject(error);
@@ -44,22 +51,25 @@ const collectionsController = {
4451
},
4552

4653
exportCollection(id) {
47-
db.collections.where('id').equals(id).first(foundCollection => {
48-
// change name and id of collection to satisfy uniqueness requirements of db
49-
foundCollection.name = foundCollection.name + " import";
50-
foundCollection.id = uuid();
54+
db.collections
55+
.where("id")
56+
.equals(id)
57+
.first((foundCollection) => {
58+
// change name and id of collection to satisfy uniqueness requirements of db
59+
foundCollection.name += " import";
60+
foundCollection.id = uuid();
5161

52-
ipcRenderer.send('export-collection', {collection: foundCollection});
53-
})
54-
.catch((error) => {
55-
console.error(error.stack || error);
56-
reject(error);
57-
});
62+
ipcRenderer.send("export-collection", { collection: foundCollection });
63+
})
64+
.catch((error) => {
65+
console.error(error.stack || error);
66+
reject(error);
67+
});
5868
},
5969

60-
importCollection() {
61-
ipcRenderer.send('import-collection');
70+
importCollection(collection) {
71+
ipcRenderer.send("import-collection", collection);
6272
},
6373
};
6474

65-
export default collectionsController;
75+
export default collectionsController;

0 commit comments

Comments
 (0)