Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest unit tests #834

Merged
merged 2 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28,467 changes: 18,679 additions & 9,788 deletions package-lock.json

Large diffs are not rendered by default.

34 changes: 31 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@
"@microsoft/sp-module-interfaces": "1.11.0",
"@microsoft/sp-tslint-rules": "1.11.0",
"@microsoft/sp-webpart-workbench": "1.11.0",
"@types/chai": "3.4.34",
"@types/chai": "^3.4.34",
"@types/chart.js": "2.7.40",
"@types/enzyme": "2.8.7",
"@types/es6-promise": "0.0.33",
"@types/jest": "^25.2.3",
"@types/lodash": "4.14.149",
"@types/mocha": "2.2.38",
"@types/react": "16.8.8",
"@types/react-addons-shallow-compare": "0.14.17",
"@types/react-addons-test-utils": "0.14.15",
Expand All @@ -62,15 +62,21 @@
"@types/webpack-env": "1.13.1",
"ajv": "~5.2.2",
"amd-loader": "0.0.8",
"chai": "^4.3.4",
"codecov": "3.6.5",
"enzyme": "2.9.1",
"enzyme": "^2.9.1",
"enzyme-adapter-react-16": "^1.15.6",
"gulp": "~3.9.1",
"husky": "4.2.5",
"identity-obj-proxy": "^3.0.0",
"jest": "^25.5.4",
"jest-cli": "^25.5.4",
"karma-html-reporter": "0.2.7",
"karma-remap-coverage": "^0.1.4",
"react-addons-test-utils": "15.6.0",
"request-promise": "4.2.5",
"sonarqube-scanner": "2.1.2",
"ts-jest": "^25.5.1",
"tslib": "2.0.0",
"webpack-bundle-analyzer": "^4.1.0"
},
Expand Down Expand Up @@ -102,5 +108,27 @@
"hooks": {
"pre-commit": "npm run changelog"
}
},
"jest": {
"transform": {
"^.+\\.(ts|tsx)$": "ts-jest"
},
"setupFilesAfterEnv": [
"./tests/setup.js"
],
"testRegex": "tests/.*\\.test.(ts?|tsx?)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"json"
],
"moduleNameMapper": {
"@ms/sp-telemetry": "identity-obj-proxy",
"@microsoft/sp-webpart-base": "identity-obj-proxy",
"@microsoft/sp-application-base": "identity-obj-proxy",
"^resx-strings/en-us.json": "<rootDir>/node_modules/@microsoft/sp-core-library/lib/resx-strings/en-us.json",
"\\.(css|scss)$": "identity-obj-proxy"
}
}
}
2 changes: 1 addition & 1 deletion src/controls/placeholder/PlaceholderComponent.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('<Placeholder />', () => {
const dummyLabel = "Dummy label";
const dummyOnConfigure = sinon.spy((evt) => { /* Nothing to do here */ });

before(() => {
beforeAll(() => {
// Suppress icon warnings.
setIconOptions({
disableWarnings: true
Expand Down
2 changes: 1 addition & 1 deletion src/webparts/controlsTest/test/ControlsTestWebPart.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// <reference types="mocha" />
/// <reference types="jest" />

import { assert } from 'chai';

Expand Down
208 changes: 208 additions & 0 deletions tests/services/SPService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
///<reference types="jest" />

import { assert } from "chai";
import { ILibsOptions, LibsOrderBy } from "../../src/services/ISPService";
import SPService from "../../src/services/SPService";

describe("SPService", () => {
test("getField (positive)", async () => {
let calledApi = "";
let ctx = {
pageContext: {
web: {
absoluteUrl: "https://test.sharepoint.com/sites/test-site"
}
},
spHttpClient: {
get: (apiUrl: string) => {
calledApi = apiUrl;
return Promise.resolve({
ok: true,
json: () => Promise.resolve({
Id: "test-field-id"
})
});
}
}
}
let spService = new SPService(ctx as any);
let field = await spService.getField("test-list-id", "test-internal-column-name");
assert.equal(field.Id, "test-field-id");
assert.equal(calledApi, "https://test.sharepoint.com/sites/test-site/_api/web/lists('test-list-id')/fields/getByInternalNameOrTitle('test-internal-column-name')");
});
test("getField (positive - calculated)", async () => {
let calledApi = "";
let ctx = {
pageContext: {
web: {
absoluteUrl: "https://test.sharepoint.com/sites/test-site"
}
},
spHttpClient: {
get: (apiUrl: string) => {
calledApi = apiUrl;
return Promise.resolve({
ok: true,
json: () => Promise.resolve({
Id: "test-field-id",
SchemaXml: `<Field Type="Calculated" DisplayName="TestCalculated" ResultType="Text"><Formula>=CONCATENATE(Title," Test")</Formula><FieldRefs><FieldRef Name="Title" /></FieldRefs></Field>`,
TypeAsString: "Calculated"
})
});
}
}
}
let spService = new SPService(ctx as any);
let field = await spService.getField("test-list-id", "test-internal-column-name");
assert.equal(field.Id, "test-field-id");
assert.equal(field.ResultType, "Text");
assert.equal(calledApi, "https://test.sharepoint.com/sites/test-site/_api/web/lists('test-list-id')/fields/getByInternalNameOrTitle('test-internal-column-name')");
});
test("getField (negative)", async () => {
let ctx = {
pageContext: {
web: {
absoluteUrl: "https://test.sharepoint.com/sites/test-site"
}
},
spHttpClient: {
get: (apiUrl: string) => {
return Promise.resolve({
ok: false,
json: () => Promise.resolve({
Id: "test-field-id"
})
});
}
}
}
let spService = new SPService(ctx as any);
let field = await spService.getField("test-list-id", "test-internal-column-name");
assert.isUndefined(field);
});
test("getField (negative [exception])", async () => {
let ctx = {
pageContext: {
web: {
absoluteUrl: "https://test.sharepoint.com/sites/test-site"
}
},
spHttpClient: {
get: (apiUrl: string) => {
throw "Test exception"
}
}
}
let spService = new SPService(ctx as any);
try {
let field = await spService.getField("test-list-id", "test-internal-column-name");
throw "No error thrown";
}
catch (err) {
assert.equal(err, "Test exception");
}
});

test.each([
[
{

},
"https://test.sharepoint.com/sites/test-site/_api/web/lists?$select=Title,id,BaseTemplate"
],
[
{
orderBy: LibsOrderBy.Id
},
"https://test.sharepoint.com/sites/test-site/_api/web/lists?$select=Title,id,BaseTemplate&$orderby=Id"
],
[
{
orderBy: LibsOrderBy.Title
},
"https://test.sharepoint.com/sites/test-site/_api/web/lists?$select=Title,id,BaseTemplate&$orderby=Title"
],
[
{
filter: "Title eq 'Test'"
},
"https://test.sharepoint.com/sites/test-site/_api/web/lists?$select=Title,id,BaseTemplate&$filter=Title%20eq%20'Test'"
],
[
{
orderBy: LibsOrderBy.Title,
filter: "Title eq 'Test'"
},
"https://test.sharepoint.com/sites/test-site/_api/web/lists?$select=Title,id,BaseTemplate&$orderby=Title&$filter=Title%20eq%20'Test'"
],
[
{
filter: "Title eq 'Test'",
baseTemplate: 101
},
"https://test.sharepoint.com/sites/test-site/_api/web/lists?$select=Title,id,BaseTemplate&$filter=Title%20eq%20'Test'"
],
[
{
baseTemplate: 101
},
"https://test.sharepoint.com/sites/test-site/_api/web/lists?$select=Title,id,BaseTemplate&$filter=BaseTemplate eq 101"
],
[
{
baseTemplate: 101,
includeHidden: false
},
"https://test.sharepoint.com/sites/test-site/_api/web/lists?$select=Title,id,BaseTemplate&$filter=BaseTemplate eq 101 and Hidden eq false"
],
[
{
includeHidden: false
},
"https://test.sharepoint.com/sites/test-site/_api/web/lists?$select=Title,id,BaseTemplate&$filter=Hidden eq false"
]
])("getLibs %j", async (options: ILibsOptions, expectedApi: string) => {
let calledApi;
let ctx = {
pageContext: {
web: {
absoluteUrl: "https://test.sharepoint.com/sites/test-site"
}
},
spHttpClient: {
get: (apiUrl: string) => {
calledApi = apiUrl;
return Promise.resolve({
ok: true,
json: () => Promise.resolve([])
});
}
}
}
let spService = new SPService(ctx as any);
await spService.getLibs(options);

assert.equal(calledApi, expectedApi);
});
test("getLibs (negative)", async () => {
let ctx = {
pageContext: {
web: {
absoluteUrl: "https://test.sharepoint.com/sites/test-site"
}
},
spHttpClient: {
get: (apiUrl: string) => {
return Promise.resolve({
ok: false,
json: () => Promise.resolve([])
});
}
}
}
let spService = new SPService(ctx as any);
let libs = await spService.getLibs({});

assert.isNull(libs);
});
});
14 changes: 14 additions & 0 deletions tests/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
jest.mock("@microsoft/sp-http", () => {
return {
SPHttpClient: {
configurations: {
v1: 1
}
},
HttpClient: {
configurations: {
v1: 1
}
}
}
});
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
],
"exclude": [
"node_modules",
"lib"
"lib",
"./tests/**"
]
}