Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for import { capabilities } from '@ember/modifier'. #10

Merged
merged 8 commits into from
Aug 23, 2019
Merged
1 change: 1 addition & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ jobs:
- ember-3.6
- ember-3.7
- ember-lts-3.8
- ember-lts-3.12
- ember-release
- ember-beta
- ember-canary
Expand Down
8 changes: 8 additions & 0 deletions config/ember-try.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ module.exports = function() {
},
},
},
{
name: 'ember-lts-3.12',
npm: {
devDependencies: {
'ember-source': '~3.12.0',
},
},
},
{
name: 'ember-release',
npm: {
Expand Down
13 changes: 8 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,24 @@ module.exports = {
let checker = new VersionChecker(this.project);
let emberVersion = checker.forEmber();

this.shouldPolyfill = emberVersion.lt('3.8.0-alpha.1');
this.shouldPolyfillManager = emberVersion.lt('3.8.0-alpha.1');
this.shouldPolyfillCapabilities = emberVersion.lt('3.13.0-beta.3');
},

included() {
this._super.included.apply(this, arguments);

if (!this.shouldPolyfill) {
return;
if (this.shouldPolyfillManager) {
this.import('vendor/ember-modifier-manager-polyfill.js');
}

this.import('vendor/ember-modifier-manager-polyfill.js');
if (this.shouldPolyfillCapabilities) {
this.import('vendor/ember-modifier-capabilities-polyfill.js');
}
},

treeForVendor(rawVendorTree) {
if (!this.shouldPolyfill) {
if (!this.shouldPolyfillManager && !this.shouldPolyfillCapabilities) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"test:all": "ember try:each"
},
"dependencies": {
"ember-cli-babel": "^7.4.2",
"ember-cli-babel": "^7.10.0",
"ember-cli-version-checker": "^2.1.2",
"ember-compatibility-helpers": "^1.2.0"
},
Expand Down
40 changes: 34 additions & 6 deletions tests/integration/components/modifier-manager-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import Ember from 'ember';
import { assign } from '@ember/polyfills';
import { capabilities, setModifierManager } from '@ember/modifier';

module('Integration | Component | modifier-manager', function(hooks) {
setupRenderingTest(hooks);
Expand All @@ -18,10 +18,34 @@ module('Integration | Component | modifier-manager', function(hooks) {
};
});

module('capabilities', function() {
test('can set capabilities', async function(assert) {
class MyModifier {}

this.owner.register('modifier:my-modifier', MyModifier);

setModifierManager(
() => ({
capabilities: capabilities('3.13'),

createModifier(factory) {
assert.equal(factory.class, MyModifier, 'the factory class is Modifier');
},
installModifier() {},
updateModifier() {},
destroyModifier() {},
}),
MyModifier
);

await render(hbs`<div {{my-modifier}}></div>`);
});
});

module('setModifierManager', function() {
test('it returns the provided object', function(assert) {
let expected = Object.freeze({});
let actual = Ember._setModifierManager(() => {}, expected);
let actual = setModifierManager(() => {}, expected);

assert.strictEqual(actual, expected, 'the passed in object was returned');
});
Expand All @@ -35,8 +59,9 @@ module('Integration | Component | modifier-manager', function(hooks) {

this.owner.register('modifier:my-modifier', MyModifier);

Ember._setModifierManager(
setModifierManager(
() => ({
capabilities: capabilities('3.13'),
createModifier(factory) {
assert.equal(factory.class, MyModifier, 'the factory class is Modifier');
},
Expand All @@ -55,8 +80,9 @@ module('Integration | Component | modifier-manager', function(hooks) {
hooks.beforeEach(function() {
class DidInsertModifier {}

Ember._setModifierManager(
setModifierManager(
() => ({
capabilities: capabilities('3.13'),
createModifier(_factory, args) {
return args.positional[0];
},
Expand Down Expand Up @@ -124,8 +150,9 @@ module('Integration | Component | modifier-manager', function(hooks) {
hooks.beforeEach(function() {
class DidUpdateModifier {}

Ember._setModifierManager(
setModifierManager(
() => ({
capabilities: capabilities('3.13'),
createModifier() {
return {};
},
Expand Down Expand Up @@ -170,8 +197,9 @@ module('Integration | Component | modifier-manager', function(hooks) {
hooks.beforeEach(function() {
class WillDestroyModifier {}

Ember._setModifierManager(
setModifierManager(
() => ({
capabilities: capabilities('3.13'),
createModifier() {
return {};
},
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/modifier-manager-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Ember from 'ember';
import { capabilities } from '@ember/modifier';
import { module, test } from 'qunit';

module('Unit | Modifier Manager', function() {
test('sets up Ember._modifierManagerCapabilities', function(assert) {
assert.strictEqual(
typeof capabilities,
'function',
'import { capabilities } from "@ember/modifier" works properly'
);

// should be transformed by babel, this just confirms it
assert.strictEqual(
capabilities,
Ember._modifierManagerCapabilities,
'typoed name matches "real" name'
);
});
});
31 changes: 31 additions & 0 deletions vendor/ember-modifier-capabilities-polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* globals Ember */
/* eslint-disable ember/new-module-imports */

import { assert, deprecate } from '@ember/debug';

(() => {
'use strict';

// Ember < 3.13 had the export typoed, this ensures both the correct location
// and the typoed location work properly
Ember._modifierManagerCapabilties = Ember._modifierManagerCapabilities = function(managerAPI) {
if (!managerAPI) {
managerAPI = '3.13';

deprecate(
'Modifier manager capabilities now require you to pass a valid version when being generated. Valid versions include: 3.13',
false,
{
until: '3.17.0',
id: 'implicit-modifier-manager-capabilities',
}
);
}

assert('Invalid modifier manager compatibility specified', managerAPI === '3.13');

// Ember 3.13 added a feature for disabling auto-tracking, but it is
// impossible to polyfill the `false` version of that
return {};
};
})();
14 changes: 14 additions & 0 deletions vendor/ember-modifier-manager-polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* eslint-disable ember/new-module-imports */

import { lte, gte } from 'ember-compatibility-helpers';
import { deprecate } from '@ember/debug';

(() => {
'use strict';
Expand Down Expand Up @@ -61,6 +62,19 @@ import { lte, gte } from 'ember-compatibility-helpers';

let instance = definition.delegate.createModifier(definition.ModifierClass, modifierArgs);

if (definition.delegate.capabilities === undefined) {
definition.delegate.capabilities = Ember._modifierManagerCapabilities('3.13');

deprecate(
'Custom modifier managers must define their capabilities using the capabilities() helper function',
false,
{
until: '3.17.0',
id: 'implicit-modifier-manager-capabilities',
}
);
}

return new CustomModifierState(element, definition.delegate, instance, capturedArgs);
}

Expand Down
Loading