Skip to content

Commit

Permalink
chore(null): null-correctness for test utils (#3035)
Browse files Browse the repository at this point in the history
#### Description of changes

Adds test utility files noted by `yarn null:find` to strict-null set.

#### Pull request checklist
<!-- If a checklist item is not applicable to this change, write "n/a" in the checkbox -->
- [x] Addresses an existing issue: #2869
- [x] Ran `yarn fastpass`
- [x] Added/updated relevant unit test(s) (and ran `yarn test`)
- [n/a] Verified code coverage for the changes made. Check coverage report at: `<rootDir>/test-results/unit/coverage`
- [x] PR title *AND* final merge commit title both start with a semantic tag (`fix:`, `chore:`, `feat(feature-name):`, `refactor:`). See `CONTRIBUTING.md`.
- [n/a] (UI changes only) Added screenshots/GIFs to description above
- [n/a] (UI changes only) Verified usability with NVDA/JAWS
  • Loading branch information
dbjorge authored Jul 7, 2020
1 parent e449579 commit 675ae80
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 36 deletions.
3 changes: 3 additions & 0 deletions src/tests/end-to-end/common/manifest-overide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export class ManifestOveride {
private modifiedManifest: chrome.runtime.Manifest;

public addTemporaryPermission(permissionToAdd: string): ManifestOveride {
if (this.modifiedManifest.permissions == null) {
this.modifiedManifest.permissions = [];
}
this.modifiedManifest.permissions.push(permissionToAdd);

return this;
Expand Down
4 changes: 3 additions & 1 deletion src/tests/end-to-end/common/pretty-print-axe-violations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { AxeResults } from 'axe-core';

export interface PrintableAxeResultNode {
selector: string[];
failureSummary: string;
Expand All @@ -10,7 +12,7 @@ export interface PrintableAxeResult {
nodes: PrintableAxeResultNode[];
}

export function prettyPrintAxeViolations(axeResults: AxeResult): PrintableAxeResult[] {
export function prettyPrintAxeViolations(axeResults: AxeResults): PrintableAxeResult[] {
const violations = axeResults.violations;
const printableViolations: PrintableAxeResult[] = violations.map(result => {
const nodeResults: PrintableAxeResultNode[] = result.nodes.map(node => {
Expand Down
33 changes: 18 additions & 15 deletions src/tests/unit/common/simulated-browser-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export function createSimulatedBrowserAdapter(
tabs: chrome.tabs.Tab[],
windows: chrome.windows.Window[],
): SimulatedBrowserAdapter {
const mock: Partial<SimulatedBrowserAdapter> = Mock.ofType<BrowserAdapter>();
const mock: Partial<SimulatedBrowserAdapter> & IMock<BrowserAdapter> = Mock.ofType<
BrowserAdapter
>();
mock.tabs = [...tabs];
mock.windows = [...windows];
mock.setup(m => m.addListenerOnConnect(It.is(isFunction))).callback(
Expand All @@ -63,13 +65,13 @@ export function createSimulatedBrowserAdapter(
c => (mock.notifyWindowsFocusChanged = c),
);

mock.setup(m => m.getRuntimeLastError()).returns(() => null);
mock.setup(m => m.getRuntimeLastError()).returns(() => undefined);
mock.setup(m => m.getAllWindows(It.isAny())).returns(() =>
Promise.resolve(mock.windows as Windows.Window[]),
);
mock.setup(m => m.getTab(It.isAny(), It.isAny(), It.isAny())).callback(
(tabId, resolve, reject) => {
const matchingTabs = mock.tabs.filter(tab => tab.id === tabId);
const matchingTabs = mock.tabs!.filter(tab => tab.id === tabId);
if (matchingTabs.length === 1) {
resolve(matchingTabs[0]);
} else if (reject != null) {
Expand All @@ -79,7 +81,7 @@ export function createSimulatedBrowserAdapter(
);

mock.setup(m => m.tabsQuery(It.isAny())).returns(query => {
const result = mock.tabs.filter(
const result = mock.tabs!.filter(
candidateTab =>
(query.active == null || query.active === candidateTab.active) &&
(query.windowId == null || query.windowId === candidateTab.windowId),
Expand All @@ -89,20 +91,21 @@ export function createSimulatedBrowserAdapter(
});

mock.updateTab = (tabId, changeInfo) => {
mock.tabs
.filter(tab => tab.id === tabId)
.forEach((tab, index) => {
mock.tabs[index] = { ...tab, ...changeInfo };
mock.notifyTabsOnUpdated(tabId, changeInfo, mock.tabs[index]);
});
mock.tabs!.filter(tab => tab.id === tabId).forEach((tab, index) => {
mock.tabs![index] = { ...tab, ...changeInfo };
mock.notifyTabsOnUpdated!(tabId, changeInfo, mock.tabs![index]);
});
};
mock.activateTab = tabToActivate => {
mock.tabs
.filter(tab => tab.windowId === tabToActivate.windowId)
.forEach((tab, index) => {
mock.tabs[index] = { ...tab, active: tabToActivate.id === tab.id };
mock.tabs!.filter(tab => tab.windowId === tabToActivate.windowId).forEach((tab, index) => {
mock.tabs![index] = { ...tab, active: tabToActivate.id === tab.id };
});
if (tabToActivate.id != null) {
mock.notifyTabsOnActivated!({
windowId: tabToActivate.windowId,
tabId: tabToActivate.id,
});
mock.notifyTabsOnActivated({ windowId: tabToActivate.windowId, tabId: tabToActivate.id });
}
};
return mock as SimulatedBrowserAdapter;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class VisualizationTogglePropsBuilder extends BaseDataBuilder<Visualizati
this.data = {
checked: false,
disabled: false,
visualizationName: null,
visualizationName: 'stub visualization name',
} as VisualizationToggleProps;
}
}
19 changes: 0 additions & 19 deletions src/tests/unit/stubs/xml-http-request-stub-builder.ts

This file was deleted.

4 changes: 4 additions & 0 deletions tsconfig.strictNullChecks.json
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,9 @@
"./src/tests/end-to-end/common/element-identifiers/popup-page-element-identifiers.ts",
"./src/tests/end-to-end/common/element-identifiers/target-page-selectors.ts",
"./src/tests/end-to-end/common/force-test-failure.ts",
"./src/tests/end-to-end/common/manifest-overide.ts",
"./src/tests/end-to-end/common/prepare-test-result-file-path.ts",
"./src/tests/end-to-end/common/pretty-print-axe-violations.ts",
"./src/tests/end-to-end/common/timeouts.ts",
"./src/tests/end-to-end/setup/test-resource-server-config.ts",
"./src/tests/end-to-end/setup/test-setup.ts",
Expand All @@ -251,9 +253,11 @@
"./src/tests/unit/common/it-is-function.ts",
"./src/tests/unit/common/node-list-builder.ts",
"./src/tests/unit/common/sample-test-data.ts",
"./src/tests/unit/common/simulated-browser-adapter.ts",
"./src/tests/unit/common/test-document-creator.ts",
"./src/tests/unit/common/tick.ts",
"./src/tests/unit/common/typemoq-helper.ts",
"./src/tests/unit/common/visualization-toggle-props-builder.ts",
"./src/tests/unit/jest-setup.ts",
"./src/tests/unit/mock-helpers/chrome-event-mock.ts",
"./src/tests/unit/mock-helpers/port-on-disconnect-mock.ts",
Expand Down

0 comments on commit 675ae80

Please sign in to comment.