fix(ui): favicon counter should render on first mount

Fixes #1761
This commit is contained in:
Łukasz Mierzwa
2020-05-19 20:19:44 +01:00
committed by Łukasz Mierzwa
parent 8cfbd741a8
commit 3e7dc8ffc3
2 changed files with 25 additions and 13 deletions

View File

@@ -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";
@@ -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

View File

@@ -14,22 +14,29 @@ beforeEach(() => {
Favico.badge.mockClear();
});
afterEach(() => {
jest.restoreAllMocks();
});
const MountedFaviconBadge = () => {
return mount(<FaviconBadge alertStore={alertStore} />);
};
describe("<FaviconBadge />", () => {
it("badge is updated when alertStore.info.totalAlerts changes", () => {
it("badge is updated on mount", () => {
alertStore.info.totalAlerts = 99;
const tree = MountedFaviconBadge();
MountedFaviconBadge();
expect(Favico.badge).toHaveBeenCalledTimes(1);
expect(Favico.badge).toHaveBeenCalledWith(99);
});
it("badge is updated when alertStore.info.totalAlerts changes", () => {
alertStore.info.totalAlerts = 99;
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", () => {
alertStore.status.error = "foo";
MountedFaviconBadge();