chore: 🤖 deprecate window object service (#1652)
* chore: 🤖 deprecate window object service Deprecate `browser-object` service in favor of `ember-browser-services` ✅ Closes: ICU-7152 * refactor: 💡 cluster-url test fail commit with cluster-url test failing ✅ Closes: ICU-7152 * refactor: 💡 refactor test to reflect changes Refactor test to reflect `browser/window` service in `cluster-url` route ✅ Closes: ICU-7152 * refactor: 💡 refactor cluster url test change logic of mockIPC to a function call ✅ Closes: ICU-7152
This commit is contained in:
1 parent
0a76111944
commit
3ba53dfc2e
8 files changed
+27
-104
No files matched your search
@@ -1,36 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) HashiCorp, Inc.
|
|
||||||
* SPDX-License-Identifier: MPL-2.0
|
|
||||||
*/
|
|
||||||
|
|
||||||
import Service from '@ember/service';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A simple service that wraps around the global window and
|
|
||||||
* document variables that allows us to mock them in testing
|
|
||||||
*/
|
|
||||||
export default class BrowserObjectService extends Service {
|
|
||||||
/**
|
|
||||||
* The global window object
|
|
||||||
* @returns {Window}
|
|
||||||
*/
|
|
||||||
get window() {
|
|
||||||
return window;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The global document object
|
|
||||||
* @returns {Document}
|
|
||||||
*/
|
|
||||||
get document() {
|
|
||||||
return this.window?.document;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The hostname of the current location of the URL
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
get hostname() {
|
|
||||||
return this.window?.location?.hostname;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) HashiCorp, Inc.
|
|
||||||
* SPDX-License-Identifier: MPL-2.0
|
|
||||||
*/
|
|
||||||
|
|
||||||
export { default } from 'core/services/browser-object';
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) HashiCorp, Inc.
|
|
||||||
* SPDX-License-Identifier: MPL-2.0
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { module, test } from 'qunit';
|
|
||||||
import { setupTest } from 'ember-qunit';
|
|
||||||
|
|
||||||
module('Unit | Service | browser-object', function (hooks) {
|
|
||||||
setupTest(hooks);
|
|
||||||
|
|
||||||
test('it contains a window', function (assert) {
|
|
||||||
let service = this.owner.lookup('service:browser-object');
|
|
||||||
assert.ok(service.window, 'Service contains a window');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('it contains a document', function (assert) {
|
|
||||||
let service = this.owner.lookup('service:browser-object');
|
|
||||||
assert.ok(service.document, 'Service contains a document');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('it contains a hostname', function (assert) {
|
|
||||||
let service = this.owner.lookup('service:browser-object');
|
|
||||||
assert.ok(service.hostname, 'Service contains a hostname');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -22,7 +22,7 @@ class Tag {
|
|||||||
export default class FormWorkerCreateWorkerLedComponent extends Component {
|
export default class FormWorkerCreateWorkerLedComponent extends Component {
|
||||||
// =services
|
// =services
|
||||||
@service features;
|
@service features;
|
||||||
@service browserObject;
|
@service('browser/window') window;
|
||||||
|
|
||||||
// =attributes
|
// =attributes
|
||||||
@tracked generatedWorkerAuthToken;
|
@tracked generatedWorkerAuthToken;
|
||||||
@@ -35,7 +35,7 @@ export default class FormWorkerCreateWorkerLedComponent extends Component {
|
|||||||
@tracked newWorkerValue;
|
@tracked newWorkerValue;
|
||||||
|
|
||||||
get clusterIDFromURL() {
|
get clusterIDFromURL() {
|
||||||
const hostname = this.browserObject.hostname;
|
const hostname = this.window?.location?.hostname;
|
||||||
|
|
||||||
// Match against a guid with either the prod, int, or dev hcp domain
|
// Match against a guid with either the prod, int, or dev hcp domain
|
||||||
const clusterIDMatcher =
|
const clusterIDMatcher =
|
||||||
|
|||||||
@@ -11,14 +11,14 @@ export default class OnboardingSuccessRoute extends Route {
|
|||||||
// =services
|
// =services
|
||||||
@service router;
|
@service router;
|
||||||
@service session;
|
@service session;
|
||||||
@service browserObject;
|
@service('browser/window') window;
|
||||||
|
|
||||||
// =methods
|
// =methods
|
||||||
model() {
|
model() {
|
||||||
const model = this.modelFor('onboarding');
|
const model = this.modelFor('onboarding');
|
||||||
return {
|
return {
|
||||||
...model,
|
...model,
|
||||||
clusterUrl: this.browserObject.window.origin,
|
clusterUrl: this.window.origin,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,11 +9,13 @@ import { render } from '@ember/test-helpers';
|
|||||||
import { hbs } from 'ember-cli-htmlbars';
|
import { hbs } from 'ember-cli-htmlbars';
|
||||||
import { v1 } from 'ember-uuid';
|
import { v1 } from 'ember-uuid';
|
||||||
import { setupIntl } from 'ember-intl/test-support';
|
import { setupIntl } from 'ember-intl/test-support';
|
||||||
|
import { setupBrowserFakes } from 'ember-browser-services/test-support';
|
||||||
|
|
||||||
module(
|
module(
|
||||||
'Integration | Component | form/worker/create-worker-led',
|
'Integration | Component | form/worker/create-worker-led',
|
||||||
function (hooks) {
|
function (hooks) {
|
||||||
setupRenderingTest(hooks);
|
setupRenderingTest(hooks);
|
||||||
|
setupBrowserFakes(hooks, { window: true });
|
||||||
setupIntl(hooks);
|
setupIntl(hooks);
|
||||||
|
|
||||||
hooks.beforeEach(function () {
|
hooks.beforeEach(function () {
|
||||||
@@ -23,14 +25,13 @@ module(
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('it correctly populates the cluster id for an hcp dev cluster', async function (assert) {
|
test('it correctly populates the cluster id for an hcp dev cluster', async function (assert) {
|
||||||
|
const windowService = this.owner.lookup('service:browser/window');
|
||||||
const guid = v1();
|
const guid = v1();
|
||||||
this.model = {};
|
this.model = {};
|
||||||
this.submit = () => {};
|
this.submit = () => {};
|
||||||
this.refresh = () => {};
|
this.refresh = () => {};
|
||||||
const service = { hostname: `${guid}.boundary.hcp.dev` };
|
|
||||||
this.owner.register('service:browser-object', service, {
|
windowService.location.hostname = `${guid}.boundary.hcp.dev`;
|
||||||
instantiate: false,
|
|
||||||
});
|
|
||||||
|
|
||||||
await render(
|
await render(
|
||||||
hbs`<Form::Worker::CreateWorkerLed @model={{this.model}} @submit={{this.submit}} @refresh={{this.refresh}} />`
|
hbs`<Form::Worker::CreateWorkerLed @model={{this.model}} @submit={{this.submit}} @refresh={{this.refresh}} />`
|
||||||
@@ -40,14 +41,13 @@ module(
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('it correctly populates the cluster id for an hcp int cluster', async function (assert) {
|
test('it correctly populates the cluster id for an hcp int cluster', async function (assert) {
|
||||||
|
const windowService = this.owner.lookup('service:browser/window');
|
||||||
const guid = v1();
|
const guid = v1();
|
||||||
this.model = {};
|
this.model = {};
|
||||||
this.submit = () => {};
|
this.submit = () => {};
|
||||||
this.refresh = () => {};
|
this.refresh = () => {};
|
||||||
const service = { hostname: `${guid}.boundary.hcp.to` };
|
|
||||||
this.owner.register('service:browser-object', service, {
|
windowService.location.hostname = `${guid}.boundary.hcp.to`;
|
||||||
instantiate: false,
|
|
||||||
});
|
|
||||||
|
|
||||||
await render(
|
await render(
|
||||||
hbs`<Form::Worker::CreateWorkerLed @model={{this.model}} @submit={{this.submit}} @refresh={{this.refresh}} />`
|
hbs`<Form::Worker::CreateWorkerLed @model={{this.model}} @submit={{this.submit}} @refresh={{this.refresh}} />`
|
||||||
@@ -57,14 +57,13 @@ module(
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('it correctly populates the cluster id for an hcp prod cluster', async function (assert) {
|
test('it correctly populates the cluster id for an hcp prod cluster', async function (assert) {
|
||||||
|
const windowService = this.owner.lookup('service:browser/window');
|
||||||
const guid = v1();
|
const guid = v1();
|
||||||
this.model = {};
|
this.model = {};
|
||||||
this.submit = () => {};
|
this.submit = () => {};
|
||||||
this.refresh = () => {};
|
this.refresh = () => {};
|
||||||
const service = { hostname: `${guid}.boundary.hashicorp.cloud` };
|
|
||||||
this.owner.register('service:browser-object', service, {
|
windowService.location.hostname = `${guid}.boundary.hashicorp.cloud`;
|
||||||
instantiate: false,
|
|
||||||
});
|
|
||||||
|
|
||||||
await render(
|
await render(
|
||||||
hbs`<Form::Worker::CreateWorkerLed @model={{this.model}} @submit={{this.submit}} @refresh={{this.refresh}} />`
|
hbs`<Form::Worker::CreateWorkerLed @model={{this.model}} @submit={{this.submit}} @refresh={{this.refresh}} />`
|
||||||
@@ -74,13 +73,12 @@ module(
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('it leaves the cluster id as blank for any other url', async function (assert) {
|
test('it leaves the cluster id as blank for any other url', async function (assert) {
|
||||||
|
const windowService = this.owner.lookup('service:browser/window');
|
||||||
this.model = {};
|
this.model = {};
|
||||||
this.submit = () => {};
|
this.submit = () => {};
|
||||||
this.refresh = () => {};
|
this.refresh = () => {};
|
||||||
const service = { hostname: `personal.website.com` };
|
|
||||||
this.owner.register('service:browser-object', service, {
|
windowService.location.hostname = `personal.website.com`;
|
||||||
instantiate: false,
|
|
||||||
});
|
|
||||||
|
|
||||||
await render(
|
await render(
|
||||||
hbs`<Form::Worker::CreateWorkerLed @model={{this.model}} @submit={{this.submit}} @refresh={{this.refresh}} />`
|
hbs`<Form::Worker::CreateWorkerLed @model={{this.model}} @submit={{this.submit}} @refresh={{this.refresh}} />`
|
||||||
|
|||||||
@@ -5,7 +5,6 @@
|
|||||||
|
|
||||||
import Route from '@ember/routing/route';
|
import Route from '@ember/routing/route';
|
||||||
import { inject as service } from '@ember/service';
|
import { inject as service } from '@ember/service';
|
||||||
import { getOwner } from '@ember/application';
|
|
||||||
import { action } from '@ember/object';
|
import { action } from '@ember/object';
|
||||||
import { loading } from 'ember-loading';
|
import { loading } from 'ember-loading';
|
||||||
import config from '../config/environment';
|
import config from '../config/environment';
|
||||||
@@ -19,6 +18,7 @@ export default class ClusterUrlRoute extends Route {
|
|||||||
@service intl;
|
@service intl;
|
||||||
@service flashMessages;
|
@service flashMessages;
|
||||||
@service router;
|
@service router;
|
||||||
|
@service('browser/window') window;
|
||||||
|
|
||||||
// =attributes
|
// =attributes
|
||||||
|
|
||||||
@@ -29,17 +29,6 @@ export default class ClusterUrlRoute extends Route {
|
|||||||
return this.store.adapterFor('application');
|
return this.store.adapterFor('application');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Looks up the window object indirectly.
|
|
||||||
* @type {Window}
|
|
||||||
*/
|
|
||||||
get window() {
|
|
||||||
// The Ember way of accessing globals...
|
|
||||||
const document = getOwner(this).lookup('service:-document').documentElement;
|
|
||||||
// defaultView === window, but without using globals directly
|
|
||||||
return document.parentNode.defaultView;
|
|
||||||
}
|
|
||||||
|
|
||||||
// =methods
|
// =methods
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -58,6 +58,10 @@ module('Acceptance | clusterUrl', function (hooks) {
|
|||||||
projects: null,
|
projects: null,
|
||||||
targets: null,
|
targets: null,
|
||||||
};
|
};
|
||||||
|
const setupMockIpc = (test) => {
|
||||||
|
test.owner.register('service:browser/window', WindowMockIPC);
|
||||||
|
mockIPC = test.owner.lookup('service:browser/window').mockIPC;
|
||||||
|
};
|
||||||
|
|
||||||
hooks.beforeEach(function () {
|
hooks.beforeEach(function () {
|
||||||
invalidateSession();
|
invalidateSession();
|
||||||
@@ -96,10 +100,6 @@ module('Acceptance | clusterUrl', function (hooks) {
|
|||||||
urls.authenticate.methods.global = `${urls.authenticate.global}/${instances.authMethods.global.id}`;
|
urls.authenticate.methods.global = `${urls.authenticate.global}/${instances.authMethods.global.id}`;
|
||||||
urls.projects = `${urls.scopes.global}/projects`;
|
urls.projects = `${urls.scopes.global}/projects`;
|
||||||
urls.targets = `${urls.projects}/targets`;
|
urls.targets = `${urls.projects}/targets`;
|
||||||
|
|
||||||
// Mock the postMessage interface used by IPC.
|
|
||||||
this.owner.register('service:browser/window', WindowMockIPC);
|
|
||||||
mockIPC = this.owner.lookup('service:browser/window').mockIPC;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
hooks.afterEach(function () {
|
hooks.afterEach(function () {
|
||||||
@@ -115,6 +115,7 @@ module('Acceptance | clusterUrl', function (hooks) {
|
|||||||
|
|
||||||
test('visiting index without a clusterUrl specified redirects to clusterUrl route', async function (assert) {
|
test('visiting index without a clusterUrl specified redirects to clusterUrl route', async function (assert) {
|
||||||
assert.expect(2);
|
assert.expect(2);
|
||||||
|
setupMockIpc(this);
|
||||||
await visit(urls.index);
|
await visit(urls.index);
|
||||||
await a11yAudit();
|
await a11yAudit();
|
||||||
assert.notOk(mockIPC.clusterUrl);
|
assert.notOk(mockIPC.clusterUrl);
|
||||||
@@ -123,6 +124,7 @@ module('Acceptance | clusterUrl', function (hooks) {
|
|||||||
|
|
||||||
test('can set clusterUrl', async function (assert) {
|
test('can set clusterUrl', async function (assert) {
|
||||||
assert.expect(3);
|
assert.expect(3);
|
||||||
|
setupMockIpc(this);
|
||||||
assert.notOk(mockIPC.clusterUrl);
|
assert.notOk(mockIPC.clusterUrl);
|
||||||
await visit(urls.clusterUrl);
|
await visit(urls.clusterUrl);
|
||||||
await fillIn('[name="host"]', currentOrigin);
|
await fillIn('[name="host"]', currentOrigin);
|
||||||
@@ -133,6 +135,7 @@ module('Acceptance | clusterUrl', function (hooks) {
|
|||||||
|
|
||||||
test('can reset clusterUrl before authentication', async function (assert) {
|
test('can reset clusterUrl before authentication', async function (assert) {
|
||||||
assert.expect(4);
|
assert.expect(4);
|
||||||
|
setupMockIpc(this);
|
||||||
assert.notOk(mockIPC.clusterUrl);
|
assert.notOk(mockIPC.clusterUrl);
|
||||||
await visit(urls.clusterUrl);
|
await visit(urls.clusterUrl);
|
||||||
await fillIn('[name="host"]', currentOrigin);
|
await fillIn('[name="host"]', currentOrigin);
|
||||||
@@ -145,6 +148,7 @@ module('Acceptance | clusterUrl', function (hooks) {
|
|||||||
|
|
||||||
test('captures error on clusterUrl update', async function (assert) {
|
test('captures error on clusterUrl update', async function (assert) {
|
||||||
assert.expect(2);
|
assert.expect(2);
|
||||||
|
setupMockIpc(this);
|
||||||
assert.notOk(mockIPC.clusterUrl);
|
assert.notOk(mockIPC.clusterUrl);
|
||||||
sinon
|
sinon
|
||||||
.stub(this.owner.lookup('service:clusterUrl'), 'setClusterUrl')
|
.stub(this.owner.lookup('service:clusterUrl'), 'setClusterUrl')
|
||||||
|
|||||||
Reference in new issue
Block a user