Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
refactor(tests): pull setup-less app-start tests up to the top level
Browse files Browse the repository at this point in the history
  • Loading branch information
philbooth committed Feb 6, 2019
1 parent b80cc24 commit 254a777
Showing 1 changed file with 148 additions and 163 deletions.
311 changes: 148 additions & 163 deletions app/tests/spec/lib/app-start.js
Expand Up @@ -75,6 +75,116 @@ define(function (require, exports, module) {
Raven.uninstall();
});

it('startApp starts the app, does not redirect', () => {
return appStart.startApp()
.then(() => {
assert.isFalse(routerMock.navigate.called);
});
});

it('startApp delegates to `fatalError` if an error occurs', () => {
const err = new Error('boom');
sinon.stub(appStart, 'allResourcesReady').callsFake(() => {
return Promise.reject(err);
});

sinon.stub(appStart, 'fatalError').callsFake(() => {});

return appStart.startApp()
.then(() => {
assert.isTrue(appStart.fatalError.calledWith(err));
});
});

it('startApp uses storage metrics when an automated browser is detected', () => {
windowMock.location.search = Url.objToSearchString({
automatedBrowser: true
});

return appStart.startApp()
.then(() => {
assert.instanceOf(appStart._metrics, StorageMetrics);
});
});

it('initializeL10n fetches translations', () => {
return appStart.initializeL10n()
.then(() => {
assert.ok(appStart._translator.fetch.calledOnce);
});
});

it('initializeErrorMetrics skips error metrics on empty config', () => {
appStart.initializeExperimentGroupingRules();
const ableChoose = sinon.stub(appStart._experimentGroupingRules, 'choose').callsFake(() => {
return true;
});

appStart.initializeErrorMetrics();
assert.isUndefined(appStart._sentryMetrics);
ableChoose.restore();
});

it('initializeErrorMetrics skips error metrics if env is not defined', () => {
appStart.initializeExperimentGroupingRules();

appStart.initializeErrorMetrics();
assert.isUndefined(appStart._sentryMetrics);
});

it('initializeErrorMetrics creates error metrics', () => {
const appStart = new AppStart({
broker: brokerMock,
config: {
env: 'development'
},
history: backboneHistoryMock,
router: routerMock,
window: windowMock
});
appStart.initializeExperimentGroupingRules();

const ableChoose = sinon.stub(appStart._experimentGroupingRules, 'choose').callsFake(() => {
return true;
});

appStart.initializeErrorMetrics();
assert.isDefined(appStart._sentryMetrics);

ableChoose.restore();
});

it('_getUniqueUserId creates a user id', () => {
assert.isDefined(appStart._getUniqueUserId());
});

it('initializeRouter creates a router', () => {
appStart.initializeRouter();
assert.isDefined(appStart._router);
});

it('initializeHeightObserver sets up the HeightObserver, triggers a `resize` notification on the iframe channel when the height changes', function (done) {
sinon.stub(appStart, '_isInAnIframe').callsFake(() => {
return true;
});

appStart._iframeChannel = {
send (message, data) {
TestHelpers.wrapAssertion(() => {
assert.equal(message, 'resize');
assert.typeOf(data.height, 'number');
}, done);
}
};

appStart.initializeHeightObserver();
});

it('initializeRefreshObserver creates a RefreshObserver instance', () => {
appStart.initializeRefreshObserver();
assert.instanceOf(appStart._refreshObserver, RefreshObserver);
});

describe('fatalError', () => {
var err;
var sandbox;
Expand Down Expand Up @@ -103,109 +213,65 @@ define(function (require, exports, module) {
});
});

describe('startApp', () => {
it('starts the app, does not redirect', () => {
return appStart.startApp()
.then(() => {
assert.isFalse(routerMock.navigate.called);
});
});
describe('with localStorage disabled', () => {
var sandbox;

it('delegates to `fatalError` if an error occurs', () => {
var err = new Error('boom');
sinon.stub(appStart, 'allResourcesReady').callsFake(() => {
return Promise.reject(err);
beforeEach(() => {
sandbox = sinon.sandbox.create();
sandbox.stub(Storage, 'isLocalStorageEnabled').callsFake(() => {
return false;
});
});

sinon.stub(appStart, 'fatalError').callsFake(() => {});
afterEach(() => {
sandbox.restore();
});

it('redirects to /cookies_disabled without history replace or trigger', () => {
return appStart.startApp()
.then(() => {
assert.isTrue(appStart.fatalError.calledWith(err));
assert.isTrue(routerMock.navigate.calledWith('cookies_disabled', {}, {replace: true, trigger: true}));
});
});

it('uses storage metrics when an automated browser is detected', () => {
windowMock.location.search = Url.objToSearchString({
automatedBrowser: true
});

it('does not redirect if path is already /cookies_disabled', () => {
windowMock.location.pathname = '/cookies_disabled';
return appStart.startApp()
.then(() => {
assert.instanceOf(appStart._metrics, StorageMetrics);
assert.isFalse(routerMock.navigate.called);
});
});

describe('with localStorage disabled', () => {
var sandbox;
it('does not redirect if Mobile Safari and /complete_signin', () => {
windowMock.navigator.userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_1_1 like Mac OS X) ' +
'AppleWebKit/602.2.14 (KHTML, like Gecko) Version/10.0 Mobile/14B100 Safari/602.1';
windowMock.location.pathname = '/complete_signin';

beforeEach(() => {
sandbox = sinon.sandbox.create();
sandbox.stub(Storage, 'isLocalStorageEnabled').callsFake(() => {
return false;
return appStart.startApp()
.then(() => {
assert.isFalse(routerMock.navigate.called);
});
});

afterEach(() => {
sandbox.restore();
});

it('redirects to /cookies_disabled without history replace or trigger', () => {
return appStart.startApp()
.then(() => {
assert.isTrue(routerMock.navigate.calledWith('cookies_disabled', {}, {replace: true, trigger: true}));
});
});

it('does not redirect if path is already /cookies_disabled', () => {
windowMock.location.pathname = '/cookies_disabled';
return appStart.startApp()
.then(() => {
assert.isFalse(routerMock.navigate.called);
});
});

it('does not redirect if Mobile Safari and /complete_signin', () => {
windowMock.navigator.userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_1_1 like Mac OS X) ' +
'AppleWebKit/602.2.14 (KHTML, like Gecko) Version/10.0 Mobile/14B100 Safari/602.1';
windowMock.location.pathname = '/complete_signin';

return appStart.startApp()
.then(() => {
assert.isFalse(routerMock.navigate.called);
});
});

it('does not redirect if Mobile Safari and /verify_email', () => {
windowMock.navigator.userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_1_1 like Mac OS X) ' +
'AppleWebKit/602.2.14 (KHTML, like Gecko) Version/10.0 Mobile/14B100 Safari/602.1';
windowMock.location.pathname = '/verify_email';

return appStart.startApp()
.then(() => {
assert.isFalse(routerMock.navigate.called);
});
});
});

it('redirects if Mobile Safari and root path', () => {
windowMock.navigator.userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_1_1 like Mac OS X) ' +
'AppleWebKit/602.2.14 (KHTML, like Gecko) Version/10.0 Mobile/14B100 Safari/602.1';
windowMock.location.pathname = '/';

return appStart.startApp()
.then(() => {
assert.isTrue(routerMock.navigate.called);
});
});
it('does not redirect if Mobile Safari and /verify_email', () => {
windowMock.navigator.userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_1_1 like Mac OS X) ' +
'AppleWebKit/602.2.14 (KHTML, like Gecko) Version/10.0 Mobile/14B100 Safari/602.1';
windowMock.location.pathname = '/verify_email';

return appStart.startApp()
.then(() => {
assert.isFalse(routerMock.navigate.called);
});
});
});

describe('initializeL10n', () => {
it('fetches translations', () => {
return appStart.initializeL10n()
it('redirects if Mobile Safari and root path', () => {
windowMock.navigator.userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_1_1 like Mac OS X) ' +
'AppleWebKit/602.2.14 (KHTML, like Gecko) Version/10.0 Mobile/14B100 Safari/602.1';
windowMock.location.pathname = '/';

return appStart.startApp()
.then(() => {
assert.ok(appStart._translator.fetch.calledOnce);
assert.isTrue(routerMock.navigate.called);
});
});
});
Expand Down Expand Up @@ -462,61 +528,6 @@ define(function (require, exports, module) {
});
});

describe('initializeErrorMetrics', () => {
it('skips error metrics on empty config', () => {
appStart.initializeExperimentGroupingRules();
var ableChoose = sinon.stub(appStart._experimentGroupingRules, 'choose').callsFake(() => {
return true;
});

appStart.initializeErrorMetrics();
assert.isUndefined(appStart._sentryMetrics);
ableChoose.restore();
});

it('skips error metrics if env is not defined', () => {
appStart.initializeExperimentGroupingRules();

appStart.initializeErrorMetrics();
assert.isUndefined(appStart._sentryMetrics);
});

it('creates error metrics', () => {
var appStart = new AppStart({
broker: brokerMock,
config: {
env: 'development'
},
history: backboneHistoryMock,
router: routerMock,
window: windowMock
});
appStart.initializeExperimentGroupingRules();

var ableChoose = sinon.stub(appStart._experimentGroupingRules, 'choose').callsFake(() => {
return true;
});

appStart.initializeErrorMetrics();
assert.isDefined(appStart._sentryMetrics);

ableChoose.restore();
});
});

describe('_getUniqueUserId', () => {
it('creates a user id', () => {
assert.isDefined(appStart._getUniqueUserId());
});
});

describe('initializeRouter', () => {
it('creates a router', () => {
appStart.initializeRouter();
assert.isDefined(appStart._router);
});
});

describe('initializeIframeChannel', () => {
beforeEach(() => {
windowMock.location.search = '?context=fx_ios_v1&service=sync&origin=' + encodeURIComponent('http://127.0.0.1:8111');
Expand All @@ -541,32 +552,6 @@ define(function (require, exports, module) {
});
});

describe('initializeHeightObserver', () => {
it('sets up the HeightObserver, triggers a `resize` notification on the iframe channel when the height changes', function (done) {
sinon.stub(appStart, '_isInAnIframe').callsFake(() => {
return true;
});

appStart._iframeChannel = {
send (message, data) {
TestHelpers.wrapAssertion(() => {
assert.equal(message, 'resize');
assert.typeOf(data.height, 'number');
}, done);
}
};

appStart.initializeHeightObserver();
});
});

describe('initializeRefreshObserver', () => {
it('creates a RefreshObserver instance', () => {
appStart.initializeRefreshObserver();
assert.instanceOf(appStart._refreshObserver, RefreshObserver);
});
});

describe('testLocalStorage', () => {
describe('with localStorage disabled', () => {
var err;
Expand Down

0 comments on commit 254a777

Please sign in to comment.