Skip to content

Commit

Permalink
fix(ui): favicon counter should render on first mount
Browse files Browse the repository at this point in the history
Fixes #1761
  • Loading branch information
prymitive committed May 19, 2020
1 parent 8cfbd74 commit 3e7dc8f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
19 changes: 12 additions & 7 deletions ui/src/Components/FaviconBadge/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState, useEffect } from "react";
import PropTypes from "prop-types";

import { autorun } from "mobx";
import { useObserver } from "mobx-react";

import Favico from "favico.js";
Expand All @@ -18,13 +19,17 @@ const FaviconBadge = ({ alertStore }) => {
})
);

useEffect(() => {
if (alertStore.status.error !== null) {
favico.badge("?");
} else {
favico.badge(alertStore.info.totalAlerts);
}
});
useEffect(
() =>
autorun(() => {
if (alertStore.status.error !== null) {
favico.badge("?");
} else {
favico.badge(alertStore.info.totalAlerts);
}
}),
[] // eslint-disable-line react-hooks/exhaustive-deps
);

return useObserver(() => (
<span
Expand Down
17 changes: 12 additions & 5 deletions ui/src/Components/FaviconBadge/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,27 @@ beforeEach(() => {
Favico.badge.mockClear();
});

afterEach(() => {
jest.restoreAllMocks();
});

const MountedFaviconBadge = () => {
return mount(<FaviconBadge alertStore={alertStore} />);
};

describe("<FaviconBadge />", () => {
it("badge is updated on mount", () => {
alertStore.info.totalAlerts = 99;
MountedFaviconBadge();
expect(Favico.badge).toHaveBeenCalledTimes(1);
expect(Favico.badge).toHaveBeenCalledWith(99);
});

it("badge is updated when alertStore.info.totalAlerts changes", () => {
alertStore.info.totalAlerts = 99;
const tree = MountedFaviconBadge();
MountedFaviconBadge();
expect(Favico.badge).toHaveBeenCalledTimes(1);
expect(Favico.badge).toHaveBeenCalledWith(99);

alertStore.info.totalAlerts = 100;
expect(Favico.badge).toHaveBeenCalledTimes(2);
expect(Favico.badge).toHaveBeenLastCalledWith(100);
});

it("badge is updated when alertStore.status.error changes", () => {
Expand Down

0 comments on commit 3e7dc8f

Please sign in to comment.