Skip to content

Commit

Permalink
test: fix test of Initializer embedded
Browse files Browse the repository at this point in the history
  • Loading branch information
MrChocolatine committed Apr 23, 2021
1 parent 98e21d8 commit 01158c7
Showing 1 changed file with 170 additions and 49 deletions.
219 changes: 170 additions & 49 deletions tests/unit/initializers/embedded-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,105 +2,226 @@ import Application from '@ember/application'

import { initialize } from 'dummy/initializers/embedded'
import { module, test } from 'qunit'
import { setupTest } from 'ember-qunit'
import Resolver from 'ember-resolver'
import { run } from '@ember/runloop'

module('Unit | Initializer | embedded', function (hooks) {
setupTest(hooks)

hooks.beforeEach(function () {
this.TestApplication = Application.extend()
this.application = this.TestApplication.create({ autoboot: false })
this.TestApplication = class TestApplication extends Application {
modulePrefix = 'something_random'
}

this.TestApplication.initializer({
name: 'initializer under test',
initialize,
})

this.application = this.TestApplication.create({ autoboot: false, Resolver })

this.application.register('config:environment', {})
})

hooks.afterEach(function () {
run(this.application, 'destroy')
})

test('by default, it does not change the normal behaviour', function (assert) {
const { _readinessDeferrals: initialDeferrals } = this.application
initialize(this.application)
assert.notOk(this.application.start, 'no method has been added')
assert.deepEqual(this.application.resolveRegistration('config:embedded'), {}, 'an empty config is registered')
assert.equal(this.application._readinessDeferrals, initialDeferrals, 'no deferral has been added')
test('by default, it does not change the normal behaviour', async function (assert) {
assert.expect(3)

await this.application.boot()

assert.strictEqual(
this.application.start,
undefined,
'No `start()` method has been added'
)

assert.deepEqual(
this.application.resolveRegistration('config:embedded'),
{},
'An empty embedded config is registered'
)

assert.ok(
this.application._booted === true && this.application._readinessDeferrals === 0,
'No deferral has been added'
)
})

test('without delegateStart, it does not change the normal behaviour', function (assert) {
test('without `delegateStart`, it does not change the normal behaviour', async function (assert) {
assert.expect(3)

this.application.register('config:environment', {
embedded: {
delegateStart: false
}
delegateStart: false,
},
})
const { _readinessDeferrals: initialDeferrals } = this.application
initialize(this.application)
assert.notOk(this.application.start, 'no method has been added')
assert.deepEqual(this.application.resolveRegistration('config:embedded'), {}, 'an empty config is registered')
assert.equal(this.application._readinessDeferrals, initialDeferrals, 'no deferral has been added')

await this.application.boot()

assert.strictEqual(
this.application.start,
undefined,
'No `start()` method has been added'
)

assert.deepEqual(
this.application.resolveRegistration('config:embedded'),
{},
'An empty embedded config is registered'
)

assert.ok(
this.application._booted === true && this.application._readinessDeferrals === 0,
'No deferral has been added'
)
})

test('without delegateStart, the specified config is registered', function (assert) {
let config = {}
test('without `delegateStart`, the specified config is registered', async function (assert) {
assert.expect(1)

const myCustomConfig = {
donald: 'duck',
}

this.application.register('config:environment', {
embedded: {
delegateStart: false,
config
}
config: myCustomConfig,
},
})
initialize(this.application)
assert.deepEqual(this.application.resolveRegistration('config:embedded'), config, 'an empty config is registered')

await this.application.boot()

assert.deepEqual(
this.application.resolveRegistration('config:embedded'),
myCustomConfig,
'The embedded config matches the custom config'
)
})

test('with delegateStart, it defers the bootstrap of the app', function (assert) {
test('with `delegateStart`, it defers the boot of the app', function (assert) {
assert.expect(3)

this.application.register('config:environment', {
embedded: {
delegateStart: true
}
delegateStart: true,
},
})

const { _readinessDeferrals: initialDeferrals } = this.application

/**
* Cannot use `application.boot()` here as this would imply triggering the readiness deferral
* and the resulting promise (of the boot) would never resolve.
*/
initialize(this.application)
assert.ok(this.application.start, 'The start method has been added')
assert.notOk(this.application.resolveRegistration('config:embedded'), 'the config is not registered until the app is started')
assert.equal(this.application._readinessDeferrals, initialDeferrals + 1, 'a deferral has been added')

assert.strictEqual(
typeof this.application.start,
'function',
'A `start()` method has been added'
)

assert.deepEqual(
this.application.resolveRegistration('config:embedded'),
undefined,
'The embedded config is not registered until the app is started'
)

assert.ok(
this.application._booted === false && this.application._readinessDeferrals === initialDeferrals + 1,
'A deferral has been added'
)
})

test('with delegateStart, the specified config is still not registered until the app starts', function (assert) {
let config = {}
test('with `delegateStart`, the passed config is not registered until the app is started', function (assert) {
assert.expect(1)

const myCustomConfig = {
donald: 'duck',
}

this.application.register('config:environment', {
embedded: {
delegateStart: true,
config
}
config: myCustomConfig,
},
})

/**
* Cannot use `application.boot()` here as this would imply triggering the readiness deferral
* and the resulting promise (of the boot) would never resolve.
*/
initialize(this.application)
assert.notOk(this.application.resolveRegistration('config:embedded'), 'no config is registered, the app is not started')

assert.deepEqual(
this.application.resolveRegistration('config:embedded'),
undefined,
'The embedded config is not registered until the app is started'
)
})

test('at manual bootstrap, the config is merged with the provided one', function (assert) {
let config = { yo: 'my config', hey: 'sup?' }
test('at manual boot, the passed config is merged into the embedded config', function (assert) {
assert.expect(1)

const myCustomConfig = {
yo: 'my config',
hey: 'sup?',
}

this.application.register('config:environment', {
embedded: {
delegateStart: true,
config
}
config: myCustomConfig,
},
})

/**
* Cannot use `application.boot()` here as this would imply triggering the readiness deferral
* and the resulting promise (of the boot) would never resolve.
*/
initialize(this.application)
this.application.start({ yay: 'one more', yo: 'new config' })
const embeddedConfig = this.application.resolveRegistration('config:embedded')
assert.equal(embeddedConfig.yo, 'new config', 'passed config overrides default config')
assert.equal(embeddedConfig.yay, 'one more', 'default keys are preserved unless overridden')
assert.equal(embeddedConfig.hey, 'sup?', 'new keys are injected')

this.application.start({
yay: 'one more',
yo: 'new config',
})

assert.deepEqual(
this.application.resolveRegistration('config:embedded'),
{
yo: 'new config',
hey: 'sup?',
yay: 'one more',
},
'The passed start config is melded into the embedded config'
)
})

test('at manual bootstrap, one deferral is removed', function (assert) {
test('at manual boot, one deferral is removed', function (assert) {
assert.expect(1)

this.application.register('config:environment', {
embedded: {
delegateStart: true
}
delegateStart: true,
},
})

/**
* Cannot use `application.boot()` here as this would imply triggering the readiness deferral
* and the resulting promise (of the boot) would never resolve.
*/
initialize(this.application)

const { _readinessDeferrals: initialDeferrals } = this.application

this.application.start()
assert.equal(this.application._readinessDeferrals, initialDeferrals - 1, 'a deferral has been removed')

assert.strictEqual(
this.application._readinessDeferrals,
initialDeferrals - 1,
'A deferral has been removed'
)
})
})

0 comments on commit 01158c7

Please sign in to comment.