Skip to content

Commit

Permalink
test(web): App
Browse files Browse the repository at this point in the history
  • Loading branch information
paularmstrong authored and blakeblackshear committed Feb 20, 2021
1 parent c12aec7 commit 3348f04
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 16 deletions.
2 changes: 2 additions & 0 deletions web/config/setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ beforeEach(() => {
throw new Error(`Unexpected fetch to ${url}, ${JSON.stringify(opts)}`);
});
});

jest.mock('../src/env');
2 changes: 1 addition & 1 deletion web/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function App() {
return (
<DarkModeProvider>
<DrawerProvider>
<div className="w-full">
<div data-testid="app" className="w-full">
<AppBar />
{status !== FetchStatus.LOADED ? (
<div className="flex flex-grow-1 min-h-screen justify-center items-center">
Expand Down
3 changes: 2 additions & 1 deletion web/src/Sidebar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { h, Fragment } from 'preact';
import LinkedLogo from './components/LinkedLogo';
import { Match } from 'preact-router/match';
import { memo } from 'preact/compat';
import { ENV } from './env';
import { useConfig } from './api';
import { useMemo } from 'preact/hooks';
import NavigationDrawer, { Destination, Separator } from './components/NavigationDrawer';
Expand Down Expand Up @@ -30,7 +31,7 @@ export default function Sidebar() {
<Destination href="/debug" text="Debug" />
<Separator />
<div className="flex flex-grow" />
{import.meta.env.MODE !== 'production' ? (
{ENV !== 'production' ? (
<Fragment>
<Destination href="/styleguide" text="Style Guide" />
<Separator />
Expand Down
2 changes: 2 additions & 0 deletions web/src/__mocks__/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const ENV = 'test';
export const API_HOST = 'http:https://base-url.local:5000';
27 changes: 27 additions & 0 deletions web/src/__tests__/App.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { h } from 'preact';
import * as Api from '../api';
import * as IDB from 'idb-keyval';
import * as PreactRouter from 'preact-router';
import App from '../App';
import { render, screen } from '@testing-library/preact';

describe('App', () => {
let mockUseConfig;

beforeEach(() => {
jest.spyOn(IDB, 'get').mockImplementation(() => Promise.resolve(undefined));
jest.spyOn(IDB, 'set').mockImplementation(() => Promise.resolve(true));
mockUseConfig = jest.spyOn(Api, 'useConfig').mockImplementation(() => ({
data: { cameras: { front: { name: 'front', objects: { track: ['taco', 'cat', 'dog'] } } } },
}));
jest.spyOn(Api, 'useApiHost').mockImplementation(() => 'http:https://base-url.local:5000');
jest.spyOn(PreactRouter, 'Router').mockImplementation(() => <div data-testid="router" />);
});

test('shows a loading indicator while loading', async () => {
mockUseConfig.mockReturnValue({ status: 'loading' });
render(<App />);
await screen.findByTestId('app');
expect(screen.queryByLabelText('Loading…')).toBeInTheDocument();
});
});
1 change: 0 additions & 1 deletion web/src/api/__mocks__/baseUrl.js

This file was deleted.

2 changes: 0 additions & 2 deletions web/src/api/__tests__/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { h } from 'preact';
import { ApiProvider, useFetch, useApiHost } from '..';
import { render, screen } from '@testing-library/preact';

jest.mock('../baseUrl');

describe('useApiHost', () => {
test('is set from the baseUrl', async () => {
function Test() {
Expand Down
3 changes: 2 additions & 1 deletion web/src/api/baseUrl.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const baseUrl = import.meta.env.SNOWPACK_PUBLIC_API_HOST || window.baseUrl || '';
import { API_HOST } from '../env';
export const baseUrl = API_HOST || window.baseUrl || '';
2 changes: 0 additions & 2 deletions web/src/components/__tests__/CameraImage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import * as Hooks from '../../hooks';
import CameraImage from '../CameraImage';
import { render, screen } from '@testing-library/preact';

jest.mock('../../api/baseUrl');

describe('CameraImage', () => {
beforeEach(() => {
jest.spyOn(Api, 'useConfig').mockImplementation(() => {
Expand Down
2 changes: 2 additions & 0 deletions web/src/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const ENV = import.meta.env.MODE;
export const API_HOST = import.meta.env.SNOWPACK_PUBLIC_API_HOST;
2 changes: 0 additions & 2 deletions web/src/routes/__tests__/Camera.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import * as Context from '../../context';
import Camera from '../Camera';
import { fireEvent, render, screen } from '@testing-library/preact';

jest.mock('../../api/baseUrl');

describe('Camera Route', () => {
let mockUsePersistence, mockSetOptions;

Expand Down
2 changes: 0 additions & 2 deletions web/src/routes/__tests__/Cameras.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import Cameras from '../Cameras';
import * as CameraImage from '../../components/CameraImage';
import { render, screen } from '@testing-library/preact';

jest.mock('../../api/baseUrl');

describe('Cameras Route', () => {
let useConfigMock;

Expand Down
2 changes: 0 additions & 2 deletions web/src/routes/__tests__/Debug.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import * as Api from '../../api';
import Debug from '../Debug';
import { render, screen } from '@testing-library/preact';

jest.mock('../../api/baseUrl');

describe('Debug Route', () => {
let useStatsMock;

Expand Down
2 changes: 0 additions & 2 deletions web/src/routes/__tests__/Event.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import * as Api from '../../api';
import Event from '../Event';
import { render, screen } from '@testing-library/preact';

jest.mock('../../api/baseUrl');

describe('Event Route', () => {
let useEventMock;

Expand Down

0 comments on commit 3348f04

Please sign in to comment.