Skip to content

Commit

Permalink
Change resolver function deprecations in to assert
Browse files Browse the repository at this point in the history
  • Loading branch information
gowtham-mk-2280 committed Dec 4, 2017
1 parent 8724cf1 commit fb9a715
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
13 changes: 11 additions & 2 deletions packages/container/lib/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { assert, deprecate } from 'ember-debug';
import { EMBER_MODULE_UNIFICATION } from 'ember/features';
import Container from './container';
import { DEBUG } from 'ember-env-flags';
import { ENV } from 'ember-environment';

const VALID_FULL_NAME_REGEXP = /^[^:]+:[^:]+$/;
let missingResolverFunctionsDeprecation = 'Passing a `resolver` function into a Registry is deprecated. Please pass in a Resolver object with a `resolve` method.';

/**
A registry used to store factory and option information keyed
Expand All @@ -24,7 +26,14 @@ export default class Registry {
this.fallback = options.fallback || null;
this.resolver = options.resolver || null;

if (typeof this.resolver === 'function') {
if (ENV._ENABLE_RESOLVER_FUNCTION_SUPPORT !== true) {
assert(
missingResolverFunctionsDeprecation,
typeof this.resolver !== 'function'
);
}

if (typeof this.resolver === 'function' && ENV._ENABLE_RESOLVER_FUNCTION_SUPPORT === true) {
deprecateResolverFunction(this);
}

Expand Down Expand Up @@ -604,7 +613,7 @@ export default class Registry {

function deprecateResolverFunction(registry) {
deprecate(
'Passing a `resolver` function into a Registry is deprecated. Please pass in a Resolver object with a `resolve` method.',
missingResolverFunctionsDeprecation,
false,
{ id: 'ember-application.registry-resolver-as-function', until: '3.0.0', url: 'https://emberjs.com/deprecations/v2.x#toc_registry-resolver-as-function' }
);
Expand Down
32 changes: 30 additions & 2 deletions packages/container/tests/registry_test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import { Registry, privatize } from '..';
import { factory } from 'internal-test-helpers';
import { EMBER_MODULE_UNIFICATION } from 'ember/features';
import { ENV } from 'ember-environment';

QUnit.module('Registry');
let originalResolverFunctionSupport;

QUnit.module('Registry', {
setup() {
originalResolverFunctionSupport = ENV._ENABLE_RESOLVER_FUNCTION_SUPPORT;
ENV._ENABLE_RESOLVER_FUNCTION_SUPPORT = true;
},

teardown() {
ENV._ENABLE_RESOLVER_FUNCTION_SUPPORT = originalResolverFunctionSupport;
}
});

QUnit.test('A registered factory is returned from resolve', function() {
let registry = new Registry();
Expand Down Expand Up @@ -473,9 +485,11 @@ QUnit.test('`knownForType` is called on the resolver if present', function() {
});
});

QUnit.test('A registry can be created with a deprecated `resolver` function instead of an object', function() {
QUnit.test('A registry created with `resolver` function instead of an object throws deprecation', function() {
expect(2);

ENV._ENABLE_RESOLVER_FUNCTION_SUPPORT = true;

let registry;

expectDeprecation(() => {
Expand All @@ -489,6 +503,20 @@ QUnit.test('A registry can be created with a deprecated `resolver` function inst
equal(registry.resolve('foo:bar'), 'foo:bar-resolved', '`resolve` still calls the deprecated function');
});

QUnit.test('A registry created with `resolver` function instead of an object throws assertion', function() {
expect(1);

let registry;

throws(() => {
registry = new Registry({
resolver(fullName) {
return `${fullName}-resolved`;
}
});
}, /Passing a `resolver` function into a Registry is deprecated. Please pass in a Resolver object with a `resolve` method./);
});

QUnit.test('resolver.expandLocalLookup is not required', function(assert) {
assert.expect(1);

Expand Down

0 comments on commit fb9a715

Please sign in to comment.