-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Waler Chau
authored and
Waler Chau
committed
Jun 5, 2017
1 parent
aa27432
commit f46580f
Showing
18 changed files
with
544 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,5 @@ [email protected] # Server-side component of the `meteor shell` comm | |
accounts-password | ||
session | ||
fourseven:scss | ||
practicalmeteor:mocha | ||
react-meteor-data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ [email protected] | |
[email protected] | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
|
@@ -54,9 +55,15 @@ [email protected] | |
[email protected] | ||
[email protected] | ||
[email protected] | ||
practicalmeteor:[email protected]_1 | ||
practicalmeteor:[email protected]_2 | ||
practicalmeteor:[email protected]_6 | ||
practicalmeteor:[email protected] | ||
practicalmeteor:[email protected]_2 | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
|
@@ -75,6 +82,8 @@ [email protected] | |
[email protected] | ||
[email protected] | ||
[email protected] | ||
tmeasday:[email protected] | ||
tmeasday:[email protected] | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import {Meteor} from 'meteor/meteor' | ||
import {Mongo} from 'meteor/mongo' | ||
|
||
import moment from 'moment' | ||
import SimpleSchema from 'simpl-schema'; | ||
|
||
export const Notes = new Mongo.Collection('notes') | ||
|
||
if(Meteor.isServer){ | ||
Meteor.publish('notes', function(){ | ||
return Notes.find({userId:this.userId}) | ||
}) | ||
} | ||
|
||
Meteor.methods({ | ||
'notes.insert'(){ | ||
if(!this.userId){ | ||
throw new Meteor.Error('not-authenticated') | ||
} | ||
return Notes.insert({ | ||
title:'', | ||
body:'', | ||
userId: this.userId, | ||
updatedAt: moment().valueOf() | ||
}) | ||
}, | ||
|
||
'notes.remove'(_id){ | ||
if(!this.userId){ | ||
throw new Meteor.Error('not-authenticated') | ||
} | ||
try{ | ||
new SimpleSchema({ | ||
_id:{ | ||
type:String, | ||
min:1 | ||
} | ||
}).validate({_id}) | ||
}catch(e){ | ||
console.log('e',e,e.message); | ||
throw new Meteor.Error(400, e.message) | ||
} | ||
|
||
Notes.remove({_id, userId:this.userId}); | ||
}, | ||
'notes.update'(_id, updates){ | ||
if(!this.userId){ | ||
throw new Meteor.Error('not-authenticated') | ||
} | ||
|
||
try{ | ||
new SimpleSchema({ | ||
_id:{ | ||
type:String, | ||
min:1 | ||
}, | ||
title:{ | ||
type:String, | ||
optional:true | ||
}, | ||
body:{ | ||
type:String, | ||
optional:true | ||
} | ||
}).validate({_id, ...updates}) | ||
}catch(e){ | ||
console.log('e',e,e.message); | ||
throw new Meteor.Error(400, e.message) | ||
} | ||
|
||
Notes.update({_id, userId:this.userId },{$set:{updatedAt: moment().valueOf(), ...updates}}) | ||
|
||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import {Meteor} from 'meteor/meteor' | ||
import expect from 'expect'; | ||
|
||
import {Notes} from './notes' | ||
|
||
if(Meteor.isServer){ | ||
describe('notes',function(){ | ||
|
||
const noteOne = { | ||
_id:'testNoteId1', | ||
title:'My Title', | ||
body:'My body for note', | ||
userId: 'testUserId1', | ||
updatedAt: 0 | ||
} | ||
|
||
const noteTwo = { | ||
_id:'testNoteId2', | ||
title:'My Title 2', | ||
body:'My body for note 2', | ||
userId: 'testUserId2', | ||
updatedAt: 0 | ||
} | ||
|
||
beforeEach(function(){ | ||
Notes.remove({}); | ||
Notes.insert(noteOne) | ||
Notes.insert(noteTwo) | ||
}) | ||
|
||
it('should insert new note', function(){ | ||
const userId = 'testId' | ||
const _id = Meteor.server.method_handlers['notes.insert'].apply({userId:'testId'}) | ||
|
||
expect(Notes.findOne({_id, userId})).toExist(); | ||
}) | ||
|
||
it('should not insert note if not authentciated',function(){ | ||
expect(()=>{ | ||
Meteor.server.method_handlers['notes.insert'](); | ||
}).toThrow(); | ||
}) | ||
|
||
it('should remove note', function(){ | ||
Meteor.server.method_handlers['notes.remove'].apply({userId:noteOne.userId},[noteOne._id]) | ||
|
||
expect(Notes.findOne({_id: noteOne._id})).toNotExist(); | ||
}) | ||
|
||
it('should not remove note if unauthenticated', function(){ | ||
expect(()=>{ | ||
Meteor.server.method_handlers['notes.remove'].apply({},[noteOne._id]) | ||
}).toThrow(); | ||
}) | ||
|
||
it('should not remove note if invalid _id', function(){ | ||
expect(()=>{ | ||
Meteor.server.method_handlers['notes.remove'].apply({userId:noteOne.userId},[]) | ||
}).toThrow(); | ||
}) | ||
|
||
it('should update note', function(){ | ||
const title = 'This is an updated title' | ||
|
||
Meteor.server.method_handlers['notes.update'].apply({userId:noteOne.userId},[noteOne._id, {title}]) | ||
|
||
const note = Notes.findOne(noteOne._id) | ||
|
||
expect(note.updatedAt).toBeGreaterThan(0) | ||
expect(note).toInclude({title,body:noteOne.body}) | ||
}) | ||
|
||
it('should throw err if extra updates', function(){ | ||
expect(()=>{ | ||
Meteor.server.method_handlers['notes.remove'].apply({userId:noteOne.userId},[noteOne._id, title:'new title', name:'name']) | ||
}).toThrow(); | ||
}) | ||
|
||
it('should not update note if user is not creater', function(){ | ||
const title = 'This is an updated title' | ||
Meteor.server.method_handlers['notes.remove'].apply({userId:'testid'},[noteOne._id, {title}]) | ||
const note = Notes.findOne(noteOne._id) | ||
expect(note).toInclude(noteOne) | ||
}) | ||
|
||
it('should not update note if unauthenticated', function(){ | ||
expect(()=>{ | ||
Meteor.server.method_handlers['notes.update'].apply({},[noteOne._id]) | ||
}).toThrow(); | ||
}) | ||
|
||
it('should not update note if invalid _id', function(){ | ||
expect(()=>{ | ||
Meteor.server.method_handlers['notes.update'].apply({userId:noteOne.userId},[]) | ||
}).toThrow(); | ||
}) | ||
|
||
it('should return a users notes', function(){ | ||
const res = Meteor.server.publish_handlers.notes.apply({userId:'testUserId2'}) | ||
const notes = res.fetch(); | ||
|
||
expect(notes.length).toBe(1) | ||
expect(notes[0]).toEqual(noteTwo) | ||
}) | ||
|
||
it('should return zero notes', function(){ | ||
const res = Meteor.server.publish_handlers.notes.apply({userId:'testUserId3'}) | ||
const notes = res.fetch(); | ||
|
||
expect(notes.length).toBe(0) | ||
}) | ||
|
||
|
||
|
||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import {Meteor} from 'meteor/meteor' | ||
import expect from 'expect' | ||
|
||
import {validateNewUser} from './users' | ||
|
||
if(Meteor.isServer){ | ||
describe('users', function(){ | ||
it('should allow valid email address',function(){ | ||
const testUser = { | ||
emails:[ | ||
{address:'[email protected]'} | ||
] | ||
} | ||
const res = validateNewUser(testUser) | ||
|
||
expect(res).toBe(true); | ||
}) | ||
|
||
it('should reject invalid email', function(){ | ||
const testUser={ | ||
emails:[ | ||
{address:'testtests'} | ||
] | ||
} | ||
|
||
expect(()=>{ | ||
validateNewUser(testUser); | ||
}).toThrow(); | ||
}) | ||
}) | ||
|
||
} | ||
|
||
// const add = (a,b) => { | ||
// if (typeof b !== 'number') return a + a; | ||
// else return a+b; | ||
// } | ||
// | ||
// const square = (a)=>{ | ||
// return a*a; | ||
// } | ||
// | ||
// describe('add', function(){ | ||
// it('should add two number', function(){ | ||
// const res = add(3,4) | ||
// // if(res!==7) throw new Error('Sum was not equal to expected value') | ||
// expect(res).toBe(20); | ||
// }); | ||
// | ||
// it('should double a single number' , function(){ | ||
// const res = add(33); | ||
// // if(res!==88) throw new Error('Number was not doubled') | ||
// expect(res).toBe(66); | ||
// }) | ||
// | ||
// }) | ||
// | ||
// describe('square', function(){ | ||
// it('should square a number', function(){ | ||
// const res = square(11); | ||
// expect(res).toBe(121); | ||
// }) | ||
// }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.