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
afterEach(async () => { | |
Thought.deleteMany({}) | |
}) | |
describe('thought model tests', () => { | |
it('can create a thought', async () => { | |
await new Thought({ message: 'test thought' }).save() | |
const thoughtsCount = await Thought.countDocuments() | |
expect(thoughtsCount).toEqual(1) | |
}), |
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
module.exports = Thought => { | |
return { | |
createThought: createThought(Thought), | |
listThoughts: listThoughts(Thought), | |
updateLikes: updateLikes(Thought) | |
} | |
} |
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
describe('updateLike test', () => { | |
it('should updates the heart count', () => { | |
const MockModel = { | |
findOneAndUpdate: sinon.spy() | |
} | |
const thoughtService = ThoughtService(MockModel) | |
thoughtService.updateLikes() | |
const actual = MockModel.findOneAndUpdate.calledOnce | |
const expected = true | |
expect(actual).toEqual(expected) |
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
it('can update likes on a thought', async () => { | |
const thought = await new Thought({ message: 'test message' }).save() | |
const fetchedThought = await Thought.findOneAndUpdate( | |
{ _id: thought._id }, | |
{ $inc: { hearts: 1 } }, | |
{ new: true } | |
) | |
expect(fetchedThought.hearts).toEqual(1) | |
}) |
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
const listThoughts = Thought => number => { | |
return Thought.find({}) | |
.sort({ _id: 'desc' }) | |
.limit(number) | |
} |
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
describe('listThoughts test', () => { | |
it('should list Thoughts', () => { | |
/* Mock model to imitate the find function */ | |
const MockModel = { | |
find: () => {} | |
} | |
/* We create a stub to imitate chained methods */ | |
sinon.stub(MockModel, 'find').callsFake(() => { | |
return { | |
sort: () => { |
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
describe('route testing', () => { | |
it('can get thoughts', async () => { | |
await request(server) | |
.get('/') | |
.expect(200) | |
}), | |
it('can post thoughts', async () => { | |
await request(server) | |
.post('/') | |
.send({ message: 'test message' }) |