Skip to content

Commit

Permalink
return values
Browse files Browse the repository at this point in the history
  • Loading branch information
Roaders committed Jun 26, 2023
1 parent 8fc3ab0 commit 241fdf0
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 21 deletions.
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
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 241fdf0

Please sign in to comment.