Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
Issue #72 add unit tests fr connected header
Browse files Browse the repository at this point in the history
  • Loading branch information
lmorchard committed Mar 6, 2019
1 parent 852529a commit a7a3b02
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/background/profile.js
Expand Up @@ -6,7 +6,7 @@ import { broadcast } from "./message-ports";

let profileInfo = null;

function updateProfileInfo(update) {
export function updateProfileInfo(update) {
profileInfo = update;
broadcast({ type: "profile_info", profile: profileInfo });
}
Expand Down
37 changes: 37 additions & 0 deletions test/unit/background/profile-test.js
@@ -0,0 +1,37 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { expect } from "chai";
import sinon from "sinon";

import "test/unit/mocks/browser";

import {
updateProfileInfo,
initializeProfileInfo,
getProfileInfo,
} from "src/background/profile";

describe("background > profile", () => {
const mockProfile = {
id: "8675309",
avatar: "https://example.com/avatar",
displayName: "John Doe",
email: "john.doe@example.com",
};

it("manages a cached profile data object", () => {
updateProfileInfo(mockProfile);
expect(getProfileInfo()).to.deep.equal(mockProfile);
});

it("listens for profile changes on initialization", async () => {
sinon.stub(browser.experiments.sync, "getUserProfileInfo").resolves(mockProfile);
await initializeProfileInfo();
expect(getProfileInfo()).to.deep.equal(mockProfile);
const updatedProfile = { ...mockProfile, displayName: "Jane Doe" };
browser.experiments.sync.onUserProfileChanged.getListener()(updatedProfile);
expect(getProfileInfo()).to.deep.equal(updatedProfile);
});
});
69 changes: 64 additions & 5 deletions test/unit/list/manage/containers/app-header-test.js
Expand Up @@ -6,11 +6,17 @@ import { expect } from "chai";
import sinon from "sinon";
import React from "react";
import mountWithL10n from "test/unit/mocks/l10n";
import { Provider } from "react-redux";
import configureStore from "redux-mock-store";

import { AppHeader } from "src/list/manage/containers/app-header";
import { initialState } from "../mock-redux-state";
import ConnectedAppHeader, { AppHeader } from "src/list/manage/containers/app-header";
import { selectTabLogins } from "src/list/actions";

describe("list > manage > containers > <AppHeader/>", () => {
const middlewares = [];
const mockStore = configureStore(middlewares);

describe("list > manage > containers > <AppHeader/>", () => {
let mockHandlers, mockDocument, listeners;

beforeEach(() => {
Expand All @@ -31,9 +37,6 @@ describe("list > manage > containers > <AppHeader/>", () => {
].forEach(name => mockHandlers[`onClick${name}`] = sinon.spy());
});

afterEach(() => {
});

const makeSubject = props => mountWithL10n(
<AppHeader {...props} />
);
Expand Down Expand Up @@ -158,3 +161,59 @@ describe("list > manage > containers > <AppHeader/>", () => {
});
});
});

describe("list > manage > containers > <ConnectedAppHeader/>", () => {
let mockSendMessage;

function makeSubject(state = initialState) {
const store = mockStore(state);
const dispatch = sinon.stub(store, "dispatch");
const subject = mountWithL10n(
<Provider store={store}>
<ConnectedAppHeader/>
</Provider>
);
return { store, subject, dispatch };
}

beforeEach(() => {
mockSendMessage = sinon.stub(browser.runtime, "sendMessage").resolves({});
});

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

it("dispatches a select action on tab click", () => {
const { subject, dispatch } = makeSubject();
const tab = subject.find("nav li button.tabLogins");
tab.simulate("click");
expect(dispatch.called).to.be.true;
expect(dispatch.firstCall.args[0]).to.deep.equal(selectTabLogins());
});

it("calls openWebsite for FAQ and Feedback menu items", () => {
const { subject } = makeSubject();

const avatarButton = subject.find("button#avatar");
avatarButton.simulate("click");

subject.find(`button[name="menuFAQ"]`).simulate("click");
subject.find(`button[name="menuFeedback"]`).simulate("click");

expect(mockSendMessage.args).to.deep.equal([
[
{
"type": "open_site",
"url": "https://lockbox.firefox.com/faq.html",
},
],
[
{
"type": "open_site",
"url": "https://qsurvey.mozilla.com/s3/Lockbox-Input?ver=2.0.0-alpha",
},
],
]);
});
});
4 changes: 4 additions & 0 deletions test/unit/mocks/browser.js
Expand Up @@ -216,5 +216,9 @@ window.browser = {
onRemoved: new MockListener(),
onAllRemoved: new MockListener(),
},
sync: {
async getUserProfileInfo() { },
onUserProfileChanged: new MockListener(),
},
},
};

0 comments on commit a7a3b02

Please sign in to comment.