Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
test.group('IoC', function () {
// Setup
ioc.fake('Adonis/Addons/Mongoose', () => require('mongoose'))
ioc.fake('Mongoose', () => ioc.use('Adonis/Addons/Mongoose'))
const BaseModel = require('../src/Model/Base')
const prov = new ServiceProvider(ioc)
prov._registerModel()
test('model should be instanciated property', function (assert) {
const Model = use('MongooseModel')
assert.exists(Model)
assert.equal(Model, BaseModel)
})
})
test('bind trait via ioc container', (assert) => {
assert.plan(1)
class FooTrait {
register (ctx) {
assert.deepEqual(ctx, User)
}
}
ioc.fake('FooTrait', () => {
return new FooTrait()
})
class User extends Model {
static boot () {
super.boot()
this.addTrait('@provider:FooTrait')
}
}
User._bootIfNotBooted()
})
test('belongsToMany work fine with IoC container binding', async (assert) => {
class Post extends Model {
}
ioc.fake('App/Models/Post', () => Post)
class User extends Model {
posts () {
return this.belongsToMany('App/Models/Post')
}
}
User._bootIfNotBooted()
Post._bootIfNotBooted()
const user = new User()
user.id = 1
user.$persisted = true
const postQuery = user.posts().toSQL()
assert.equal(postQuery.sql, helpers.formatQuery('select "posts".*, "post_user"."post_id" as "pivot_post_id", "post_user"."user_id" as "pivot_user_id" from "posts" inner join "post_user" on "posts"."id" = "post_user"."post_id" where "post_user"."user_id" = ?'))
})
test('remove listener for any event binded via ioc container', (assert) => {
const event = new Event(new Config())
const stack = []
ioc.fake('App/Listeners/Event', () => {
return {
occurs: (event) => {
stack.push(event)
}
}
})
event.any('Event.occurs')
assert.lengthOf(event.getListenersAny(), 1)
event.offAny('Event.occurs')
assert.lengthOf(event.getListenersAny(), 0)
})
test('use same named middleware twice with different params', async (assert) => {
class AppMiddleware {
async handle (ctx, next, [id]) {
ctx.ids = ctx.ids || []
ctx.ids.push(id)
await next()
}
}
ioc.fake('Middleware/AppMiddleware', function () {
return new AppMiddleware()
})
Route.get('/', async function ({ ids }) {
return ids
})
.middleware('app:1')
.middleware('app:2')
this.server.registerNamed({
'app': 'Middleware/AppMiddleware'
})
const app = http.createServer(this.server.handle.bind(this.server))
const res = await supertest(app).get('/').expect(200)
test('add middleware to resource', async (assert) => {
const Route = use('Route')
ioc.fake('App/Controllers/Http/UserController', function () {
return new UserController()
})
Route
.resource('users', 'UserController')
.middleware([
async ({ response }, next) => {
await next()
response._lazyBody.content = response._lazyBody.content + ' via middleware'
}
])
assert.equal((await supertest(appUrl).get('/users').expect(200)).text, 'all via middleware')
assert.equal((await supertest(appUrl).get('/users/create').expect(200)).text, 'form via middleware')
assert.equal((await supertest(appUrl).post('/users').expect(200)).text, 'stored via middleware')
assert.equal((await supertest(appUrl).get('/users/1').expect(200)).text, 'one via middleware')
test('auto pic hook from pre-defined directory', async (assert) => {
const hooks = new Hooks()
const stack = []
ioc.fake('App/Models/Hooks/Foo', () => {
return {
bar () {
stack.push(1)
}
}
})
hooks.addHandler('save', 'Foo.bar')
await hooks.exec('create')
assert.deepEqual(stack, [1])
})
group.before(() => {
ioc.fake('Task', () => require('./../src/Scheduler/Task'))
ioc.fake('Adonis/Src/Logger', () => new Logger())
})
group.beforeEach(() => {
this.exception = Exception
this.server = new Server(Context, Route, new Logger(), this.exception)
this.server.bindExceptionHandler()
ioc.fake('Adonis/Exceptions/BaseExceptionHandler', () => {
return new BaseExceptionHandler()
})
})
pictureable () {
return this.morphTo()
}
}
ioc.fake('App/Models/User', () => {
User._bootIfNotBooted()
return User
})
ioc.fake('App/Models/Post', () => {
Post._bootIfNotBooted()
return Post
})
ioc.fake('App/Models/Picture', () => {
Picture._bootIfNotBooted()
return Picture
})
const rs = await ioc.use('Database').collection('users').insert({ username: 'virk' })
const rsPost = await ioc.use('Database').collection('post').insert({ title: 'test post' })
const rsPicture = await ioc.use('Database').collection('pictures').insert([{ parent_id: rs.insertedIds[0], determiner: 'User', path: '/foo' }, { parent_id: rsPost.insertedIds[0], determiner: 'Post', path: '/foo' }])
const picture = await Picture.find(rsPicture.insertedIds[0])
const pictureable = await picture.pictureable().fetch()
assert.instanceOf(pictureable, User)
})