Skip to content

Commit

Permalink
Merge pull request #84 from Roaders/propertyVerifiers
Browse files Browse the repository at this point in the history
Return getter and setter verifiers from property setup functions
  • Loading branch information
Roaders authored Jul 19, 2023
2 parents 2e51036 + d4d2f4b commit 9667227
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = {
plugins: ['@typescript-eslint'],
ignorePatterns: ['dist/', 'reports/', 'docs/'],
rules: {
'prettier/prettier': ['error', prettierConfig],
'prettier/prettier': ['error', { trailingComma: 'all', tabWidth: 4, singleQuote: true, printWidth: 120 }],
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unused-vars': 'off',
Expand Down
14 changes: 10 additions & 4 deletions main/mock/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,10 @@ export interface IMocked<T, C extends ConstructorFunction<T> = never> {
* @param propertyname
* @param value
*/
setupProperty<K extends keyof T>(propertyname: K, value?: T[K]): IFunctionVerifier<T, 'getter', C>;
setupProperty<K extends keyof T>(
propertyname: K,
value?: T[K],
): { getter: IFunctionVerifier<T, 'getter', C>; setter: IFunctionVerifier<T, 'setter', C> };
/**
* Defines a single property and allows getters and setters to be defined.
* Returns a function verifier to verify get and set operations
Expand All @@ -189,7 +192,7 @@ export interface IMocked<T, C extends ConstructorFunction<T> = never> {
propertyname: K,
getter?: () => T[K],
setter?: (value: T[K]) => void,
): IFunctionVerifier<T, 'getter', C>;
): { getter: IFunctionVerifier<T, 'getter', C>; setter: IFunctionVerifier<T, 'setter', C> };

/**
* Sets up a single static function and returns a function verifier to verify calls made and parameters passed.
Expand All @@ -207,7 +210,10 @@ export interface IMocked<T, C extends ConstructorFunction<T> = never> {
* @param propertyname
* @param value
*/
setupStaticProperty<K extends keyof C>(propertyname: K, value?: C[K]): IFunctionVerifier<T, 'staticGetter', C>;
setupStaticProperty<K extends keyof C>(
propertyname: K,
value?: C[K],
): { getter: IFunctionVerifier<T, 'staticGetter', C>; setter: IFunctionVerifier<T, 'staticSetter', C> };
/**
* Defines a single static property and allows getters and setters to be defined.
* Returns a function verifier to verify get and set operations
Expand All @@ -220,7 +226,7 @@ export interface IMocked<T, C extends ConstructorFunction<T> = never> {
propertyname: K,
getter?: () => C[K],
setter?: (value: C[K]) => void,
): IFunctionVerifier<T, 'staticGetter', C>;
): { getter: IFunctionVerifier<T, 'staticGetter', C>; setter: IFunctionVerifier<T, 'staticSetter', C> };

/**
* Verifies calls to a previously setup function.
Expand Down
8 changes: 4 additions & 4 deletions main/mock/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ export class Mock {
},
setupProperty: <K extends keyof T>(propertyName: K, value?: T[K]) => {
setupProperty<T, C, K>(propertyName, value)(mocked);
return mocked.withGetter(propertyName);
return { getter: mocked.withGetter(propertyName), setter: mocked.withSetter(propertyName) };
},
defineProperty: <K extends keyof T>(
propertyName: K,
getter?: () => T[K],
setter?: (value: T[K]) => void,
) => {
defineProperty<T, C, K>(propertyName, getter, setter)(mocked);
return mocked.withGetter(propertyName);
return { getter: mocked.withGetter(propertyName), setter: mocked.withSetter(propertyName) };
},

setupStaticFunction: <K extends keyof FunctionsOnly<C>>(functionName: K, mockFunction?: any) => {
Expand All @@ -58,15 +58,15 @@ export class Mock {
},
setupStaticProperty: <K extends keyof C>(propertyName: K, value?: C[K]) => {
setupStaticProperty<T, C, K>(propertyName, value)(mocked);
return mocked.withStaticGetter(propertyName);
return { getter: mocked.withStaticGetter(propertyName), setter: mocked.withStaticSetter(propertyName) };
},
defineStaticProperty: <K extends keyof C>(
propertyName: K,
getter?: () => C[K],
setter?: (value: C[K]) => void,
) => {
defineStaticProperty<T, C, K>(propertyName, getter, setter)(mocked);
return mocked.withStaticGetter(propertyName);
return { getter: mocked.withStaticGetter(propertyName), setter: mocked.withStaticSetter(propertyName) };
},

withFunction: <U extends keyof FunctionsOnly<T>>(functionName: U) =>
Expand Down
2 changes: 1 addition & 1 deletion main/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../tsconfig.json",
"exclude": [
"dist"
"../dist"
],
"files": [
"index.ts"
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@morgan-stanley/ts-mocking-bird",
"version": "0.7.0",
"version": "1.0.0",
"description": "A fully type safe mocking, call verification and import replacement library for jasmine and jest",
"license": "Apache-2.0",
"author": "Morgan Stanley",
Expand All @@ -13,15 +13,14 @@
"copy": "copyfiles main/**/*.html main/**/*.css main/**/*.jpg dist",
"prebuild-release": "npm run clean && npm run verify-release",
"verify-release": "npm run build && concurrently --kill-others-on-fail npm:lint npm:test npm:test:jest",
"build-release": "npm run copy-dist && typedoc main/index.ts",
"watch-build": "tsc --watch",
"build-release": "typedoc main/index.ts",
"watch-build": "tsc --watch -p main/tsconfig.json",
"test": "karma start --singleRun --browsers ChromeHeadlessNoSandbox",
"test:karma": "karma start --singleRun --browsers ChromeHeadlessNoSandbox",
"test:jest": "jest",
"lint": "eslint . --ext .ts,.js",
"watch-test": "karma start --no-coverage",
"watch-test:jest": "jest --watch",
"copy-dist": "concurrently --kill-others-on-fail npm:copy-dist-*",
"watch-test-coverage": "karma start",
"lint:fix": "eslint . --ext .ts,.js --fix",
"compatibility-test": "npx tsc spec/ts-compat-test.ts --noEmit --skipLibCheck --watch"
Expand Down
6 changes: 0 additions & 6 deletions prettier.config.js

This file was deleted.

38 changes: 29 additions & 9 deletions spec/mock/mock-static.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -700,20 +700,40 @@ describe('mock with statics', () => {
});

describe('defineStaticProperty', () => {
it('called directly on mock instance', () => {
mocked.setup(defineStaticProperty('propertyOne'));
describe('getter', () => {
it('called directly on mock instance', () => {
mocked.setup(defineStaticProperty('propertyOne'));

get(mocked.mockConstructor.propertyOne);
get(mocked.mockConstructor.propertyOne);

expect(mocked.withStaticGetter('propertyOne')).wasCalledAtLeastOnce();
expect(mocked.withStaticGetter('propertyOne')).wasCalledAtLeastOnce();
});

it('called on checker returned from setup function', () => {
const verifier = mocked.defineStaticProperty('propertyOne').getter;

get(mocked.mockConstructor.propertyOne);

expect(verifier).wasCalledAtLeastOnce();
});
});

it('called on checker returned from setup function', () => {
const verifier = mocked.defineStaticProperty('propertyOne');
describe('setter', () => {
it('called directly on mock instance', () => {
mocked.setup(defineStaticProperty('propertyOne'));

get(mocked.mockConstructor.propertyOne);
mocked.mockConstructor.propertyOne = 'one';

expect(verifier).wasCalledAtLeastOnce();
expect(mocked.withStaticSetter('propertyOne')).wasCalledAtLeastOnce();
});

it('called on checker returned from setup function', () => {
const verifier = mocked.defineStaticProperty('propertyOne').setter;

mocked.mockConstructor.propertyOne = 'one';

expect(verifier).wasCalledAtLeastOnce();
});
});
});

Expand All @@ -727,7 +747,7 @@ describe('mock with statics', () => {
});

it('called on checker returned from setup function', () => {
const verifier = mocked.setupStaticProperty('propertyOne');
const verifier = mocked.setupStaticProperty('propertyOne').getter;

get(mocked.mockConstructor.propertyOne);

Expand Down
52 changes: 44 additions & 8 deletions spec/mock/mock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ describe('mock', () => {
});

it('called on checker returned from setup function', () => {
const verifier = mocked.setupProperty('propertyOne');
const verifier = mocked.setupProperty('propertyOne').getter;

get(mock.propertyOne);

Expand Down Expand Up @@ -1098,24 +1098,60 @@ describe('mock', () => {
});

describe('defineProperty', () => {
describe('getter', () => {
it('called directly on mock instance', () => {
mocked.setup(defineProperty('propertyOne'));

get(mock.propertyOne);

expect(mocked.withGetter('propertyOne')).wasCalledAtLeastOnce();
});

it('called on checker returned from setup function', () => {
const verifier = mocked.defineProperty('propertyOne').getter;

get(mock.propertyOne);

expect(verifier).wasCalledAtLeastOnce();
});
});

describe('setter', () => {
it('called directly on mock instance', () => {
mocked.setup(defineProperty('propertyOne'));

mock.propertyOne = 'one';

expect(mocked.withSetter('propertyOne')).wasCalledAtLeastOnce();
});

it('called on checker returned from setup function', () => {
const verifier = mocked.defineProperty('propertyOne').setter;

mock.propertyOne = 'one';

expect(verifier).wasCalledAtLeastOnce();
});
});
});

describe('withSetter', () => {
it('called directly on mock instance', () => {
mocked.setup(defineProperty('propertyOne'));
mocked.setup(setupProperty('propertyOne'));

get(mock.propertyOne);
mock.propertyOne = 'one';

expect(mocked.withGetter('propertyOne')).wasCalledAtLeastOnce();
expect(mocked.withSetter('propertyOne')).wasCalledAtLeastOnce();
});

it('called on checker returned from setup function', () => {
const verifier = mocked.defineProperty('propertyOne');
const verifier = mocked.setupProperty('propertyOne').setter;

get(mock.propertyOne);
mock.propertyOne = 'one';

expect(verifier).wasCalledAtLeastOnce();
});
});

describe('withSetter', () => {
describe('assertion with parameters', () => {
beforeEach(() => {
mocked.setup(defineProperty('propertyOne'));
Expand Down

0 comments on commit 9667227

Please sign in to comment.