Skip to content

Commit

Permalink
add tests for external API usage
Browse files Browse the repository at this point in the history
- as CommonJS (require)
- as import in Webpack
- as import in TypeScript with typings

code is backported from 3.x alpha (cec3d27)
  • Loading branch information
warpech committed Aug 8, 2019
1 parent bdd319f commit 2ccf621
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ npm-debug.log

# Typescript
*.d.ts
test/spec/typings/typingsSpec.js

# compiled files
src/*.js
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@
"build": "tsc && webpack",
"serve": "http-server -p 5000 --silent",
"tsc-watch": "tsc -w",
"test": "npm run tsc && npm run test-core && npm run test-duplex",
"test": "npm run tsc && npm run test-core && npm run test-duplex && npm run test-commonjs && npm run test-webpack-import && npm run test-typings",
"test-sauce": "npm run build && node test/Sauce/Runner.js",
"test-commonjs": "jasmine test/spec/commonjs/requireSpec.js",
"test-webpack-import": "webpack --env.NODE_ENV=test && jasmine test/spec/webpack/importSpec.build.js",
"test-typings": "tsc test/spec/typings/typingsSpec.ts",
"test-duplex": "jasmine DUPLEX=yes JASMINE_CONFIG_PATH=test/jasmine.json",
"test-core": "jasmine DUPLEX=no JASMINE_CONFIG_PATH=test/jasmine.json test/spec/jsonPatchTestsSpec.js test/spec/coreSpec.js test/spec/validateSpec.js",
"bench": "npm run bench-core && npm run bench-duplex",
Expand Down
2 changes: 1 addition & 1 deletion test/jasmine.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"spec_dir": "test",
"spec_files": [
"**/*[sS]pec.js"
"spec/*[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
Expand Down
24 changes: 24 additions & 0 deletions test/spec/commonjs/requireSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const jsonpatch = require('../../..');

describe('CommonJS', function () {
describe('require', function () {
it('should have the expected structure', function () {
expect(typeof jsonpatch).withContext("result from require() should be an object").toEqual("object");
expect(typeof jsonpatch).withContext("result from require() should not be a function").not.toEqual("function");
expect(jsonpatch.applyOperation).withContext("applyOperation should be a method within the object").toBeDefined();
expect(jsonpatch.applyPatch).withContext("applyPatch should be a method within the object").toBeDefined();
expect(jsonpatch.applyReducer).withContext("applyReducer should be a method within the object").toBeDefined();
expect(jsonpatch.getValueByPointer).withContext("getValueByPointer should be a method within the object").toBeDefined();
expect(jsonpatch.validate).withContext("validate should be a method within the object").toBeDefined();
expect(jsonpatch.validator).withContext("validator should be a method within the object").toBeDefined();
expect(jsonpatch.JsonPatchError).withContext("JsonPatchError should be a method within the object").toBeDefined();
expect(jsonpatch.deepClone).withContext("deepClone should be a method within the object").toBeDefined();
expect(jsonpatch.escapePathComponent).withContext("escapePathComponent should be a method within the object").toBeDefined();
expect(jsonpatch.unescapePathComponent).withContext("unescapePathComponent should be a method within the object").toBeDefined();
expect(jsonpatch.unobserve).withContext("unobserve should be a method within the object").toBeDefined();
expect(jsonpatch.observe).withContext("observe should be a method within the object").toBeDefined();
expect(jsonpatch.generate).withContext("generate should be a method within the object").toBeDefined();
expect(jsonpatch.compare).withContext("compare should be a method within the object").toBeDefined();
});
});
});
22 changes: 22 additions & 0 deletions test/spec/typings/typingsSpec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Run using `npm run test-typings`
* The sole fact that this file compiles means that typings work
* This follows how DefinitelyTyped tests work
* @see https://stackoverflow.com/questions/49296151/how-to-write-tests-for-typescript-typing-definition
*/

// import jsonpatch from '../../..';
import * as jsonpatchStar from '../../..';
import { applyPatch, Operation } from '../../..';

const document = { firstName: "Albert", contactDetails: { phoneNumbers: [] } };

const typedPatch = new Array<Operation>({ op: "replace", path: "/firstName", value: "Joachim" });
const untypedPatch = [{ op: "replace", path: "/firstName", value: "Joachim" }];

// const test_jsonpatch = jsonpatch.applyPatch(document, typedPatch).newDocument;
const test_jsonpatchStar = jsonpatchStar.applyPatch(document, typedPatch).newDocument;
const test_applyPatch = applyPatch(document, typedPatch).newDocument;

// the below line would NOT compile with TSC
// const test_applyPatch = applyPatch(document, untypedPatch).newDocument;
13 changes: 13 additions & 0 deletions test/spec/webpack/importSpec.build.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2ccf621

Please sign in to comment.