Skip to content

Commit

Permalink
fix: add types (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulsattar authored Oct 25, 2023
1 parent 08ff837 commit abd8583
Show file tree
Hide file tree
Showing 13 changed files with 458 additions and 188 deletions.
14 changes: 12 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
{
"extends": ["@salesforce/eslint-config-lwc/recommended"]
}
"parser": "@typescript-eslint/parser",
"parserOptions": {
"sourceType": "module"
},

"plugins": ["jest", "@typescript-eslint", "import"],
"extends": ["eslint:recommended", "plugin:@typescript-eslint/eslint-recommended"],
"rules": {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }]
}
}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
*.log
.DS_Store
dist/*.js
dist
node_modules
coverage
.vscode
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"printWidth": 100,
"singleQuote": true,
"tabWidth": 4
}
134 changes: 71 additions & 63 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,65 +1,73 @@
{
"name": "@salesforce/wire-service-jest-util",
"version": "4.0.2",
"description": "Utility library emit test data through @wire adapters in Lightning web component tests",
"license": "MIT",
"main": "dist/wire-service-jest-util.common.js",
"module": "dist/wire-service-jest-util.es.js",
"files": [
"dist/*"
],
"scripts": {
"prepare": "yarn build",
"check-license-headers": "node ./scripts/checkLicenseHeaders.js",
"clean": "rimraf dist node_modules",
"lint": "eslint src",
"build": "rollup -c ./rollup.config.mjs",
"test": "jest --coverage",
"release:version": "npm version",
"release:publish": "npm publish --access public --registry=https://registry.npmjs.org",
"release:publish:ci": "node ./scripts/release/publish.js"
},
"keywords": [
"lwc",
"wire-service",
"@wire",
"jest",
"testing"
],
"repository": "https://github.com/salesforce/wire-service-jest-util",
"peerDependencies": {
"@lwc/engine-dom": ">=2.0.0"
},
"devDependencies": {
"@babel/core": "^7.22.10",
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@lwc/compiler": "^3.1.3",
"@lwc/engine-dom": "^3.1.3",
"@lwc/eslint-plugin-lwc": "^1.6.3",
"@lwc/jest-preset": "^13.0.0",
"@lwc/synthetic-shadow": "^3.1.3",
"@rollup/plugin-babel": "^6.0.3",
"@salesforce/eslint-config-lwc": "^3.5.2",
"@salesforce/eslint-plugin-lightning": "^1.0.0",
"eslint": "^8.46.0",
"eslint-plugin-import": "^2.28.0",
"eslint-plugin-jest": "^27.2.3",
"isbinaryfile": "^5.0.0",
"jest": "^29.6.2",
"jest-environment-jsdom": "^29.6.2",
"rimraf": "^5.0.1",
"rollup": "^3.28.0"
},
"lwc": {
"modules": [
{
"name": "wire-service-jest-util",
"path": "dist/wire-service-jest-util.es.js"
}
]
},
"volta": {
"node": "18.18.0",
"yarn": "1.22.19"
}
"name": "@salesforce/wire-service-jest-util",
"version": "4.0.2",
"description": "Utility library emit test data through @wire adapters in Lightning web component tests",
"license": "MIT",
"main": "dist/wire-service-jest-util.common.js",
"module": "dist/wire-service-jest-util.es.js",
"files": [
"dist/*",
"types.d.ts"
],
"types": "dist/dts/index.d.ts",
"scripts": {
"prepare": "yarn build",
"check-license-headers": "node ./scripts/checkLicenseHeaders.js",
"clean": "rimraf dist node_modules",
"lint": "eslint src",
"build": "rollup -c ./rollup.config.mjs",
"test": "jest --coverage",
"release:version": "npm version",
"release:publish": "npm publish --access public --registry=https://registry.npmjs.org",
"release:publish:ci": "node ./scripts/release/publish.js"
},
"keywords": [
"lwc",
"wire-service",
"@wire",
"jest",
"testing"
],
"repository": "https://github.com/salesforce/wire-service-jest-util",
"peerDependencies": {
"@lwc/engine-dom": ">=2.0.0"
},
"devDependencies": {
"@babel/core": "^7.22.10",
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@lwc/compiler": "^3.1.3",
"@lwc/engine-dom": "^3.1.3",
"@lwc/eslint-plugin-lwc": "^1.6.3",
"@lwc/jest-preset": "^13.0.0",
"@lwc/synthetic-shadow": "^3.1.3",
"@rollup/plugin-babel": "^6.0.3",
"@rollup/plugin-typescript": "^11.1.5",
"eslint": "^8.46.0",
"eslint-plugin-import": "^2.28.0",
"eslint-plugin-jest": "^27.2.3",
"isbinaryfile": "^5.0.0",
"jest": "^29.6.2",
"jest-environment-jsdom": "^29.6.2",
"rimraf": "^5.0.1",
"rollup": "^3.28.0",
"tslib": "^2.6.2",
"typescript": "^5.2.2"
},
"lwc": {
"modules": [
{
"name": "wire-service-jest-util",
"path": "dist/wire-service-jest-util.es.js"
}
]
},
"volta": {
"node": "18.18.0",
"yarn": "1.22.19"
},
"dependencies": {
"@types/jest": "^29.5.6",
"@typescript-eslint/eslint-plugin": "^6.8.0",
"@typescript-eslint/parser": "^6.8.0"
}
}
12 changes: 7 additions & 5 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import { babel } from '@rollup/plugin-babel';
import typescript from '@rollup/plugin-typescript';

export default {
input: 'src/index.js',
input: 'src/index.ts',
plugins: [
typescript(),
babel({
babelHelpers: 'bundled',
plugins: [['@babel/plugin-proposal-class-properties']]
plugins: [['@babel/plugin-proposal-class-properties']],
}),
],
output: [
Expand All @@ -21,7 +23,7 @@ export default {
},
{
format: 'cjs',
file: 'dist/wire-service-jest-util.common.js'
}
]
file: 'dist/wire-service-jest-util.common.js',
},
],
};
11 changes: 7 additions & 4 deletions src/__tests__/index.test.js → src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,37 @@
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import * as target from '../index.js';
import * as target from '../index';

describe('registerLdsTestWireAdapter', () => {
it('returns a test wire adapter', () => {
const testWireAdapter = target.registerLdsTestWireAdapter(()=>{});
const testWireAdapter = target.registerLdsTestWireAdapter(() => {});
expect(testWireAdapter).toHaveProperty('emit');
expect(testWireAdapter).toHaveProperty('error');
expect(testWireAdapter).toHaveProperty('getLastConfig');
});

it('throws error when no adapter id', () => {
expect(() => {
// @ts-ignore because we aren't passing any argument here and TS fails
// but in JS, since there's no check, this test is necessary
target.registerTestWireAdapter();
}).toThrow('No adapter specified');
});
});

describe('registerTestWireAdapter', () => {
it('returns a test wire adapter', () => {
const testWireAdapter = target.registerTestWireAdapter(()=>{});
const testWireAdapter = target.registerTestWireAdapter(() => {});
expect(testWireAdapter).toHaveProperty('emit');
expect(testWireAdapter).not.toHaveProperty('error');
expect(testWireAdapter).toHaveProperty('getLastConfig');
});

it('throws error when no adapter id', () => {
expect(() => {
// @ts-ignore same reason as above
target.registerTestWireAdapter();
}).toThrow('No adapter specified');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/

import { TestWireAdapterTemplate } from "./TestWireAdapter";
import { TestWireAdapterTemplate } from './TestWireAdapter';

function buildErrorObject({ body, status, statusText }) {
function buildErrorObject({
body,
status,
statusText,
}: {
body?: any;
status?: number;
statusText?: string;
}) {
if (status && (status < 400 || status > 599)) {
throw new Error("'status' must be >= 400 or <= 599");
}
Expand All @@ -29,23 +37,26 @@ function buildErrorObject({ body, status, statusText }) {
}

class ApexTestWireAdapterTemplate extends TestWireAdapterTemplate {
static emit(value, filterFn) {
static emit(value: any, filterFn?: (config: Record<string, any>) => boolean) {
super.emit({ data: value, error: undefined }, filterFn);
}

static emitError(errorOptions, filterFn) {
static emitError(
errorOptions: { body?: any; status?: number; statusText?: string },
filterFn: (config: Record<string, any>) => boolean
) {
const err = buildErrorObject(errorOptions || {});

super.emit({ data: undefined, error: err }, filterFn);
}

static error(body, status, statusText) {
static error(body?: any, status?: number, statusText?: string) {
const err = buildErrorObject({ body, status, statusText });

super.emit({ data: undefined, error: err });
}

constructor(dataCallback) {
constructor(dataCallback: (value: any) => void) {
super(dataCallback);

this.emit({ data: undefined, error: undefined });
Expand All @@ -55,6 +66,6 @@ class ApexTestWireAdapterTemplate extends TestWireAdapterTemplate {
export function buildApexTestWireAdapter() {
return class ApexTestWireAdapter extends ApexTestWireAdapterTemplate {
static _lastConfig = null;
static _wireInstances = new Set();
}
}
static _wireInstances = new Set<ApexTestWireAdapter>();
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,27 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/

import { TestWireAdapterTemplate } from "./TestWireAdapter";
import { TestWireAdapterTemplate } from './TestWireAdapter';

function buildErrorObject({ body, status, statusText }) {
function buildErrorObject({
body,
status,
statusText,
}: {
body?: any;
status?: number;
statusText?: string;
}) {
if (status && (status < 400 || status > 599)) {
throw new Error("'status' must be >= 400 or <= 599");
}

body = body || [{
errorCode: 'NOT_FOUND',
message: 'The requested resource does not exist',
}];
body = body || [
{
errorCode: 'NOT_FOUND',
message: 'The requested resource does not exist',
},
];

status = status || 404;

Expand All @@ -30,32 +40,35 @@ function buildErrorObject({ body, status, statusText }) {
}

class LdsTestWireAdapterTemplate extends TestWireAdapterTemplate {
static emit(value, filterFn) {
static emit(value: any, filterFn?: (config: Record<string, any>) => boolean) {
super.emit({ data: value, error: undefined }, filterFn);
}

static emitError(errorOptions, filterFn) {
static emitError(
errorOptions: { body?: any; status?: number; statusText?: string },
filterFn?: (config: Record<string, any>) => boolean
) {
const err = buildErrorObject(errorOptions || {});

super.emit({ data: undefined, error: err }, filterFn);
}

static error(body, status, statusText) {
static error(body?: any, status?: number, statusText?: string) {
const err = buildErrorObject({ body, status, statusText });

super.emit({ data: undefined, error: err });
}

constructor(dataCallback) {
constructor(dataCallback: (value: any) => void) {
super(dataCallback);

this.emit({ data: undefined, error: undefined })
this.emit({ data: undefined, error: undefined });
}
}

export function buildLdsTestWireAdapter() {
return class LdsTestWireAdapter extends LdsTestWireAdapterTemplate {
static _lastConfig = null;
static _wireInstances = new Set();
}
}
static _wireInstances = new Set<LdsTestWireAdapter>();
};
}
Loading

0 comments on commit abd8583

Please sign in to comment.