Skip to content

Commit

Permalink
Add Flow to remaining files
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtatum committed Jul 13, 2018
1 parent 44aab64 commit 83114bc
Show file tree
Hide file tree
Showing 18 changed files with 174 additions and 311 deletions.
2 changes: 1 addition & 1 deletion src/components/app/MenuButtons.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ class ProfileSharingCompositeButton extends PureComponent<
resolve(jsonString);
})
.then((s: string) => new TextEncoder().encode(s))
.then((typedArray: $TypedArray) => {
.then((typedArray: Uint8Array) => {
return Promise.all([compress(typedArray.slice(0)), sha1(typedArray)]);
})
.then(([gzipData, hash]: [string, string]) => {
Expand Down
6 changes: 3 additions & 3 deletions src/test/fixtures/mocks/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

// @flow
export function createImageMock() {
function Image() {
function ImageMock() {
instances.push(this);
}
const instances = [];
return { instances, Image };
const instances: Image[] = [];
return { instances, Image: ImageMock };
}
13 changes: 10 additions & 3 deletions src/test/setup.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
/* 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/. */
// @flow

import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new Adapter() });

jest.mock('../utils/worker-factory');
// Disabling eslint for this line because this property is only part of the mock
import { shutdownWorkers } from '../utils/worker-factory'; // eslint-disable-line import/named
import * as WorkerFactory from '../utils/worker-factory';

afterEach(function() {
shutdownWorkers();
// This `__shutdownWorkers` function only exists in the mocked test environment,
// do not use flow typing on it.
const { __shutdownWorkers } = (WorkerFactory: Object);
__shutdownWorkers();
});
108 changes: 60 additions & 48 deletions src/test/store/icons.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* 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/. */
// @flow

import { createImageMock } from '../fixtures/mocks/image';
import { blankStore } from '../fixtures/stores';
import * as iconsAccessors from '../../reducers/icons';
import * as iconsActions from '../../actions/icons';
import type { CallNodeDisplayData } from '../../types/profile-derived';

describe('actions/icons', function() {
const validIcons = [
Expand All @@ -18,85 +20,84 @@ describe('actions/icons', function() {
];
const invalidIcon = 'https://invalid.icon.example.org/favicon.ico';

let instances;
let imageInstances: Image[] = [];

beforeEach(() => {
const mock = createImageMock();
instances = mock.instances;
window.Image = mock.Image;
imageInstances = mock.instances;
(window: Object).Image = mock.Image;
});

afterEach(() => {
delete window.Image;
instances = null;
delete (window: Object).Image;
imageInstances = [];
});

let store;

beforeEach(function() {
store = blankStore();
});

afterEach(function() {
store = null;
});
function _createCallNodeWithIcon(icon: string): CallNodeDisplayData {
return {
totalTime: '0',
totalTimePercent: '0',
selfTime: '0',
name: 'icon',
lib: 'icon',
dim: false,
icon,
};
}

describe('With the initial state', function() {
let state;
beforeEach(function() {
state = store.getState();
});

afterEach(function() {
state = null;
});

function getInitialState() {
return blankStore().getState();
}
it('getIcons return an empty set', function() {
const initialState = iconsAccessors.getIcons(state);
const initialState = iconsAccessors.getIcons(getInitialState());
expect(initialState).toBeInstanceOf(Set);
expect(initialState.size).toEqual(0);
});

it('getIconForCallNode returns null for any icon', function() {
const subject = iconsAccessors.getIconForCallNode(state, {
icon: validIcons[0],
});
const subject = iconsAccessors.getIconForCallNode(
getInitialState(),
_createCallNodeWithIcon(validIcons[0])
);
expect(subject).toBeNull();
});

it('getIconClassNameForCallNode returns an empty string for any icon', function() {
const subject = iconsAccessors.getIconClassNameForCallNode(state, {
icon: validIcons[0],
});
const subject = iconsAccessors.getIconClassNameForCallNode(
getInitialState(),
_createCallNodeWithIcon(validIcons[0])
);
expect(subject).toBe('');
});

it('getIconsWithClassNames returns an empty array', function() {
const subject = iconsAccessors.getIconsWithClassNames(state);
const subject = iconsAccessors.getIconsWithClassNames(getInitialState());
expect(subject).toEqual([]);
});
});

describe('Requesting an existing icon', function() {
it('will populate the local cache', async function() {
const { dispatch, getState } = blankStore();
const promises = [
store.dispatch(iconsActions.iconStartLoading(validIcons[0])),
dispatch(iconsActions.iconStartLoading(validIcons[0])),
// Second request for the same icon shouldn't dspatch anything
store.dispatch(iconsActions.iconStartLoading(validIcons[0])),
dispatch(iconsActions.iconStartLoading(validIcons[0])),
// 3rd request for another icon should dispatch the loaded action
store.dispatch(iconsActions.iconStartLoading(validIcons[1])),
dispatch(iconsActions.iconStartLoading(validIcons[1])),
];

// Only 2 requests because only 2 different icons
expect(instances.length).toBe(2);
instances.forEach((instance, i) => {
expect(imageInstances.length).toBe(2);
imageInstances.forEach((instance, i) => {
expect(instance.src).toEqual(validIcons[i]);
expect(instance.referrerPolicy).toEqual('no-referrer');
});
instances.forEach(instance => instance.onload());
imageInstances.forEach(instance => (instance: Object).onload());
await Promise.all(promises);

const state = store.getState();
const state = getState();
let subject = iconsAccessors.getIcons(state);
expect([...subject]).toEqual(validIcons);

Expand All @@ -106,35 +107,46 @@ describe('actions/icons', function() {
);

validIcons.forEach((icon, i) => {
subject = iconsAccessors.getIconForCallNode(state, { icon });
subject = iconsAccessors.getIconForCallNode(
state,
_createCallNodeWithIcon(icon)
);
expect(subject).toEqual(icon);

subject = iconsAccessors.getIconClassNameForCallNode(state, { icon });
subject = iconsAccessors.getIconClassNameForCallNode(
state,
_createCallNodeWithIcon(icon)
);
expect(subject).toEqual(expectedClasses[i]);
});
});
});

describe('Requesting a non-existing image', function() {
it('will not populate the local cache', async function() {
const actionPromise = store.dispatch(
const { dispatch, getState } = blankStore();
const actionPromise = dispatch(
iconsActions.iconStartLoading(invalidIcon)
);
expect(instances.length).toBe(1);
instances[0].onerror();
expect(imageInstances.length).toBe(1);
(imageInstances[0]: Object).onerror();

await actionPromise;

const state = store.getState();
const state = getState();
let subject = iconsAccessors.getIcons(state);
expect([...subject]).toEqual([]);

subject = iconsAccessors.getIconForCallNode(state, { icon: invalidIcon });
subject = iconsAccessors.getIconForCallNode(
state,
_createCallNodeWithIcon(invalidIcon)
);
expect(subject).toBeNull();

subject = iconsAccessors.getIconClassNameForCallNode(state, {
icon: invalidIcon,
});
subject = iconsAccessors.getIconClassNameForCallNode(
state,
_createCallNodeWithIcon(invalidIcon)
);
expect(subject).toBe('');

subject = iconsAccessors.getIconsWithClassNames(state);
Expand Down
1 change: 1 addition & 0 deletions src/test/unit/function-info.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* 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/. */
// @flow

import {
stripFunctionArguments,
Expand Down
1 change: 1 addition & 0 deletions src/test/unit/symbol-store-db.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* 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/. */
// @flow

import 'babel-polyfill';
import SymbolStoreDB from '../../profile-logic/symbol-store-db';
Expand Down
16 changes: 10 additions & 6 deletions src/test/unit/symbol-store.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* 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/. */
// @flow

import 'babel-polyfill';
import { SymbolStore } from '../../profile-logic/symbol-store';
Expand All @@ -9,6 +10,7 @@ import exampleSymbolTable from '../fixtures/example-symbol-table';
import fakeIndexedDB from 'fake-indexeddb';
import FDBKeyRange from 'fake-indexeddb/lib/FDBKeyRange';
import { FakeSymbolStore } from '../fixtures/fake-symbol-store';
import { ensureExists } from '../../utils/flow';

describe('SymbolStore', function() {
let symbolProvider, symbolStore;
Expand Down Expand Up @@ -57,7 +59,7 @@ describe('SymbolStore', function() {
expect(symbolProvider.requestSymbolTableFromAddon).not.toHaveBeenCalled();

const lib1 = { debugName: 'firefox', breakpadId: 'dont-care' };
let secondAndThirdSymbol;
let secondAndThirdSymbol = new Map();
await symbolStore.getSymbols(
[{ lib: lib1, addresses: new Set([0xf01, 0x1a50]) }],
(request, results) => {
Expand All @@ -77,7 +79,7 @@ describe('SymbolStore', function() {
});

const lib2 = { debugName: 'firefox2', breakpadId: 'dont-care2' };
let firstAndLastSymbol;
let firstAndLastSymbol = new Map();
await symbolStore.getSymbols(
[{ lib: lib2, addresses: new Set([0x33, 0x2000]) }],
(request, results) => {
Expand Down Expand Up @@ -196,19 +198,21 @@ describe('SymbolStore', function() {
// requestSymbolTableFromAddon should have been called for it, once.
expect(symbolProvider.requestSymbolTableFromAddon).toHaveBeenCalledTimes(1);

expect(symbolsPerLibrary.get(lib1).get(0xf01)).toEqual({
const lib1Symbols = ensureExists(symbolsPerLibrary.get(lib1));
const lib2Symbols = ensureExists(symbolsPerLibrary.get(lib2));
expect(lib1Symbols.get(0xf01)).toEqual({
name: 'second symbol',
functionOffset: 1,
});
expect(symbolsPerLibrary.get(lib1).get(0x1a50)).toEqual({
expect(lib1Symbols.get(0x1a50)).toEqual({
name: 'third symbol',
functionOffset: 0x50,
});
expect(symbolsPerLibrary.get(lib2).get(0x33)).toEqual({
expect(lib2Symbols.get(0x33)).toEqual({
name: 'first symbol',
functionOffset: 0x33,
});
expect(symbolsPerLibrary.get(lib2).get(0x2000)).toEqual({
expect(lib2Symbols.get(0x2000)).toEqual({
name: 'last symbol',
functionOffset: 0,
});
Expand Down
1 change: 1 addition & 0 deletions src/types/globals/ClipboardEvent.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* 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/. */
// @flow

declare class ClipboardEvent extends Event {
clipboardData: DataTransfer;
Expand Down
1 change: 1 addition & 0 deletions src/types/globals/DOMRect.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* 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/. */
// @flow

declare class DOMRect {
constructor(x: ?number, y: ?number, width: ?number, height: ?number): DOMRect;
Expand Down
1 change: 1 addition & 0 deletions src/types/globals/Image.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* 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/. */
// @flow

declare class Image extends HTMLImageElement {
constructor(width?: number, height?: number): void;
Expand Down
1 change: 1 addition & 0 deletions src/types/globals/WheelEvent.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* 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/. */
// @flow

declare class WheelEvent extends MouseEvent {
deltaX: number; // readonly
Expand Down
7 changes: 7 additions & 0 deletions src/types/globals/Window.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ declare class Window extends EventTarget {
) => CSSStyleDeclaration;
TextDecoder: typeof TextDecoder;
setTimeout: typeof setTimeout;
crypto: {
// This is a definition of only the methods we use.
// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
subtle: {
digest: (string, Uint8Array) => Promise<ArrayBuffer>,
},
};
fetch: typeof fetch;
DOMRect: typeof DOMRect;
requestIdleCallback: typeof requestIdleCallback;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* 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/. */
// @flow

// copied from https://github.com/flowtype/flow-typed/pull/2063
declare type $$reactsplitterlayout$$Props = {|
Expand Down
12 changes: 10 additions & 2 deletions src/utils/__mocks__/worker-factory.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/* 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/. */
// @flow

import Worker from 'workerjs';

const workerFiles = {
Expand All @@ -8,14 +13,17 @@ const workerFiles = {
const workerInstances = [];

export default class {
constructor(file) {
constructor(file: string) {
const worker = new Worker(workerFiles[file]);
workerInstances.push(worker);
return worker;
}
}

export function shutdownWorkers() {
/**
* This function allows for stopping the workers, and is only part of the mock.
*/
export function __shutdownWorkers() {
workerInstances.forEach(worker => worker.terminate());
workerInstances.length = 0;
}
Loading

0 comments on commit 83114bc

Please sign in to comment.