Skip to content

Instantly share code, notes, and snippets.

Viktor Dahl tehvicke

Block or report user

Report or block tehvicke

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@tehvicke
tehvicke / app.test.js
Created Feb 14, 2020
Endpoint testing
View app.test.js
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' })
@tehvicke
tehvicke / thought-service.test.js
Created Feb 14, 2020
Testing a service with mock models and a stub
View thought-service.test.js
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: () => {
@tehvicke
tehvicke / thought-service.js
Created Feb 14, 2020
List thought function
View thought-service.js
const listThoughts = Thought => number => {
return Thought.find({})
.sort({ _id: 'desc' })
.limit(number)
}
@tehvicke
tehvicke / thought-model.test.js
Created Feb 14, 2020
Test to validate that we can update an object in the database
View thought-model.test.js
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)
})
@tehvicke
tehvicke / thought-service.test.js
Created Feb 14, 2020
Simple test using sinon in a mock model to listen on whether a function was called or not
View thought-service.test.js
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)
@tehvicke
tehvicke / thought-service.js
Created Feb 14, 2020
The exported higher order functions
View thought-service.js
module.exports = Thought => {
return {
createThought: createThought(Thought),
listThoughts: listThoughts(Thought),
updateLikes: updateLikes(Thought)
}
}
@tehvicke
tehvicke / thought-model.test.js
Created Feb 14, 2020
Simple test to ensure data can be created in the database
View thought-model.test.js
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)
}),
You can’t perform that action at this time.