Skip to content

Commit

Permalink
Merge ba26739 into 629ddd3
Browse files Browse the repository at this point in the history
  • Loading branch information
ro0gr committed Nov 5, 2019
2 parents 629ddd3 + ba26739 commit 9c0ec1a
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 49 deletions.
6 changes: 6 additions & 0 deletions addon-test-support/-private/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ export function render(template) {
* @return {PageObject} - the page object
*/
export function setContext(context) {
deprecate('setContext() is deprecated. Please make sure you use "@ember/test-helpers" of v1 or higher.', false, {
id: 'ember-cli-page-object.set-context',
until: '2.0.0',
url: 'https://ember-cli-page-object.js.org/docs/v1.16.x/deprecations/#set-context',
});

if (context) {
this.context = context;
}
Expand Down
23 changes: 17 additions & 6 deletions addon-test-support/-private/execution_context.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getContext as getIntegrationTestContext } from './helpers';
import { getContext, visit } from './compatibility';
import { getContext as getEmberTestHelpersContext, visit } from './compatibility';
import AcceptanceExecutionContext from './execution_context/acceptance';
import IntegrationExecutionContext from './execution_context/integration';
import Rfc268Context from './execution_context/rfc268';
Expand Down Expand Up @@ -28,10 +28,14 @@ export function getExecutionContext(pageObjectNode) {
let contextName;
if (integrationTestContext) {
contextName = 'integration';
} else if (isRfc268Test()) {
contextName = 'rfc268';
} else {
} else if (isAcceptanceTest()) {
contextName = 'acceptance';
} else if (supportsRfc268()) {
contextName = 'rfc268';
}

if (!contextName) {
throw new Error('Can not detect test type. Please make sure you use the latest version of "@ember/test-helpers".');
}

return new executioncontexts[contextName](pageObjectNode, integrationTestContext);
Expand All @@ -40,7 +44,14 @@ export function getExecutionContext(pageObjectNode) {
/**
* @private
*/
export function isRfc268Test() {
function isAcceptanceTest() {
return window.visit && window.andThen;
}

/**
* @private
*/
export function supportsRfc268() {
// `getContext()` returns:
// - falsey, if @ember/test-helpers is not available (stubbed in
// compatibility.js)
Expand All @@ -53,7 +64,7 @@ export function isRfc268Test() {
// Note that if `page.setContext(this)` has been called, we'll never get here
// and will just be running with the integration context (even if the test is
// an RFC268 test).
let hasValidTestContext = Boolean(getContext());
let hasValidTestContext = Boolean(getEmberTestHelpersContext());
if (!hasValidTestContext) {
return false;
}
Expand Down
16 changes: 11 additions & 5 deletions addon-test-support/-private/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { get } from '@ember/object';
import { isPresent } from '@ember/utils';
import Ceibo from 'ceibo';
import { deprecate } from '@ember/application/deprecations';
import { getContext as getEmberTestHelpersContext } from './compatibility';

import $ from '-jquery';

Expand Down Expand Up @@ -196,20 +197,25 @@ export function getRoot(node) {
/**
* @public
*
* Return a test context if one was provided during `create()`
* Return a test context if one was provided during `create()` or via `setContext()`
*
* @param {Ceibo} node - Node of the tree
* @return {?Object} The test's `this` context, or null
* @return {Object} `moduleForComponent` test's `this` context, or null
*/
export function getContext(node) {
let root = getRoot(node);
let { context } = root;

if (typeof context === 'object' && typeof context.$ === 'function') {
if (typeof context === 'object' && context !== null && typeof context.$ === 'function') {
return context;
} else {
return null;
}

context = getEmberTestHelpersContext();
if (typeof context === 'object' && context !== null && typeof context.$ === 'function' && !context.element) {
return context
}

return null;
}

function getAllValuesForProperty(node, property) {
Expand Down
10 changes: 6 additions & 4 deletions addon-test-support/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ import dsl from './-private/dsl';
function buildObject(node, blueprintKey, blueprint, defaultBuilder) {
let definition;

// to allow page objects to exist in definitions, we store the definition that
// to allow page objects to exist in definitions, we store the definition that
// created the page object, allowing us to substitute a page object with its
// definition during creation
// definition during creation
if (isPageObject(blueprint)) {
definition = getPageObjectDefinition(blueprint);
} else {
Expand All @@ -65,7 +65,7 @@ function buildObject(node, blueprintKey, blueprint, defaultBuilder) {

// persist definition once we have an instance
storePageObjectDefinition(instance, blueprintToStore);

return [ instance, blueprintToApply ];
}

Expand Down Expand Up @@ -206,7 +206,9 @@ export function create(definitionOrUrl, definitionOrOptions, optionsOrNothing) {
page.setContext = setContext;
page.removeContext = removeContext;

page.setContext(context);
if (typeof context !== 'undefined') {
page.setContext(context);
}
}

return page;
Expand Down
21 changes: 21 additions & 0 deletions guides/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@ title: Deprecations

This is a list of deprecations introduced in 1.x cycle:

## Set context

**ID**: ember-cli-page-object.set-context

**Until**: 2.0.0

With "@ember@test-helpers@1.0.0" or higher you don't need to set page object context anymore, it would be handled for you:

Bad:

```js
page.setContext(this);
page.removeContext(this);

const page = create({
context: this
});
```

Good: Make sure you are using the most recent version of "@ember/test-helpers" and remove setting of context from your test suite.

## Comma-separated Selectors

**ID**: ember-cli-page-object.comma-separated-selectors
Expand Down
5 changes: 4 additions & 1 deletion tests/helpers/properties/integration-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import $ from '-jquery';
export { moduleForComponent as moduleForIntegration, test as testForIntegration } from 'ember-qunit';
import expectEmberError from '../../expect-ember-error';
import hbs from 'htmlbars-inline-precompile';
import { supportsRfc268 } from 'ember-cli-page-object/test-support/-private/execution_context';

export function IntegrationAdapter(context) {
this.context = context;
Expand Down Expand Up @@ -31,7 +32,9 @@ IntegrationAdapter.prototype = {
test.set('raw', template);
}

page.setContext(test);
if (!supportsRfc268()) {
page.setContext(test);
}

this.context.render(hbs`{{html-render html=raw}}`);
},
Expand Down
58 changes: 31 additions & 27 deletions tests/integration/deprecations/legacy-collection-test.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
import { moduleForComponent, test } from 'ember-qunit';
import { module, test } from 'ember-qunit';
import { setupTest } from 'ember-qunit';

import { create, collection } from 'ember-cli-page-object';

moduleForComponent('calculating-device', 'Deprecation | legacy collection', {
integration: true
});
import require from 'require';
if (require.has('@ember/test-helpers')) {
module('Deprecation | legacy collection', function(hooks) {
setupTest(hooks);

test('shows deprecation warning when first parameter is not a string', function(assert) {
let page = create({
foo: collection({
itemScope: 'li'
})
});
test('shows deprecation warning when first parameter is not a string', function(assert) {
let page = create({
foo: collection({
itemScope: 'li'
})
});

page.foo();
page.foo();

assert.expectDeprecation('You are currently using the legacy collection API, check the documentation to see how to upgrade to the new API.');
});
assert.expectDeprecation('You are currently using the legacy collection API, check the documentation to see how to upgrade to the new API.');
});

test("doesn't show a deprecation warning when first parameter is a string", function(assert) {
let page = create({
foo: collection('foo')
});
test("doesn't show a deprecation warning when first parameter is a string", function(assert) {
let page = create({
foo: collection('foo')
});

page.foo;
page.foo;

assert.expectNoDeprecation();
});
assert.expectNoDeprecation();
});

test('shows a warning on invalid legacy collection definitions', function(assert) {
assert.expectWarning(function() {
create({
foo: collection({
})
test('shows a warning on invalid legacy collection definitions', function(assert) {
assert.expectWarning(function() {
create({
foo: collection({
})
});
}, 'Legacy page object collection definition is invalid. Please, make sure you include a `itemScope` selector.');
});
}, 'Legacy page object collection definition is invalid. Please, make sure you include a `itemScope` selector.');
});
});
}
23 changes: 23 additions & 0 deletions tests/integration/deprecations/set-context-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { module, test } from 'qunit';
import { create } from 'ember-cli-page-object';
import require from 'require';

if (require.has('@ember/test-helpers')) {
const DEPRECATION_MESSAGE = 'setContext() is deprecated. Please make sure you use "@ember/test-helpers" of v1 or higher.';

module('Deprecation | setContext', function() {
test('passing page context to create is deprecated', function(assert) {
this.page = create().setContext(this);

assert.expectDeprecation(DEPRECATION_MESSAGE)
});

test('passing page context to create is deprecated', function(assert) {
this.page = create({
context: this
});

assert.expectDeprecation(DEPRECATION_MESSAGE)
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { setupTest } from 'ember-qunit';
import require from 'require';

if (require.has('@ember/test-helpers')) {
module('Unit | is rfc268 test', function(hooks) {
function isRfc268Test() {
module('Unit | supports rfc268', function(hooks) {
function supportsRfc268() {
window.require.unsee('ember-cli-page-object/test-support/-private/execution_context');
return require('ember-cli-page-object/test-support/-private/execution_context').isRfc268Test();
return require('ember-cli-page-object/test-support/-private/execution_context').supportsRfc268();
}

hooks.afterEach(function() {
Expand All @@ -30,18 +30,19 @@ if (require.has('@ember/test-helpers')) {
});

test('works', function(assert) {
assert.ok(isRfc268Test());
assert.ok(supportsRfc268());
});

test('throws without visit() present', function(assert) {
compatModule.visit = undefined;
assert.throws(isRfc268Test);

assert.throws(() => supportsRfc268());
});
});

module('without context', function() {
test('works', function(assert) {
assert.notOk(isRfc268Test());
assert.notOk(supportsRfc268());
});
});
});
Expand Down

0 comments on commit 9c0ec1a

Please sign in to comment.