Skip to content

Instantly share code, notes, and snippets.

@HugoPoi
Created June 18, 2018 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HugoPoi/8b376d409a2bf786845cbd23f85542d4 to your computer and use it in GitHub Desktop.
Save HugoPoi/8b376d409a2bf786845cbd23f85542d4 to your computer and use it in GitHub Desktop.
Loopback sample mocha unit test
'use strict';
const loopback = require('loopback');
const expect = require('chai').expect;
const request = require('supertest');
describe('Test relations update support', function() {
// Create a new loopback app.
const app = loopback()
// Register the mixin
app.loopback.modelBuilder.mixins.define('IncludeOnUpdate', require('../include-on-update'))
app.loopback.modelBuilder.mixins.define('HasAndBelongsToManyUpdate', require('../hasAndBelongsToMany-update'))
const City = loopback.PersistedModel.extend('City', {
name: String,
}, {
plural: 'Cities',
mixins: {
IncludeOnUpdate: {},
},
relations: {
country: {
type: 'belongsTo',
model: 'Country',
foreignKey: 'countryId',
},
},
})
const Country = loopback.PersistedModel.extend('Country', {
name: String,
}, { plural: 'Countries'})
const Person = loopback.PersistedModel.extend('Person', {
name: String,
}, {
plural: 'People',
mixins: {
HasAndBelongsToManyUpdate: {},
IncludeOnUpdate: {},
},
relations: {
liveIn: {
type: 'hasAndBelongsToMany',
model: 'City'
}
}
})
app.model(City)
app.model(Country)
app.model(Person)
const dbConnector = loopback.memory();
City.attachTo(dbConnector);
Country.attachTo(dbConnector);
Person.attachTo(dbConnector);
app.use('/api', loopback.rest());
beforeEach(function clearMemoryConnector(done){
dbConnector.automigrate(done);
});
it('create belongsTo', function() {
return app.models.Country.create({name: 'France'})
.then(country => {
return request(app).post('/api/Cities')
.send({ name: 'Paris', country: country })
.expect(200)
.set('Accept', 'application/json')
.then(response => {
expect(response.body.country).to.be.a('object');
expect(response.body.id).to.equal(1);
});
});
});
it('update belongsTo', function() {
return app.models.Country.create({name: 'Belgium'})
.then(country => {
return request(app).put('/api/Cities')
.send({ name: 'Paris', country: country })
.expect(200)
.set('Accept', 'application/json')
.then(response => {
expect(response.body.country).to.be.a('object');
expect(response.body.country).to.have.property('name').to.be.equal('Belgium');
expect(response.body.id).to.equal(1);
})
.then(() => City.findById(1, {include: 'country'}))
.then(city => {
city = city.toJSON();
expect(city.country).to.be.a('object');
expect(city.country).to.have.property('name').to.be.equal('Belgium');
})
});
});
it('create hasAndBelongsToMany', function() {
return app.models.City.create([{name: 'Paris'}, {name: 'New York'}])
.then(cities => {
return request(app).post('/api/People')
.send({ name: 'Jean-Claude', liveIn: cities })
.expect(200)
.set('Accept', 'application/json')
.then(response => {
expect(response.body.liveIn).to.be.a('array');
expect(response.body.liveIn).to.have.lengthOf(2);
expect(response.body.id).to.equal(1);
})
.then(() => app.models.Person.findOne().then(person => person.liveIn.find()))
.then(liveIn => {
expect(liveIn).to.be.a('array');
expect(liveIn).to.have.lengthOf(2);
});
});
});
it('update hasAndBelongsToMany', function() {
return app.models.City.create([{name: 'Paris'}, {name: 'New York'}])
.then(() => app.models.Person.create({ name: 'Yve' }))
.then(person => {
person = person.toJSON();
person.liveIn = [{id: 1, name: 'Paris'}];
return request(app).put('/api/People')
.send(person)
.expect(200)
.set('Accept', 'application/json')
.then(response => {
expect(response.body.liveIn).to.be.a('array');
expect(response.body.liveIn).to.have.lengthOf(1);
expect(response.body.id).to.equal(1);
})
.then(() => app.models.Person.findOne().then(person => person.liveIn.find()))
.then(liveInCountries => {
expect(liveInCountries).to.be.a('array');
expect(liveInCountries).to.have.lengthOf(1);
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment