Skip to content

Commit

Permalink
feat: mock property type support symbol and number (#3)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Enhanced mocking functionality to support a broader range of key
types, including symbols and numbers.
- Added new test cases for mocking Symbol properties and number
properties.

- **Bug Fixes**
- Updated tests to ensure original properties are correctly restored
after mocking.

- **Documentation**
- Updated test suite documentation to reflect changes in key handling
and new test cases.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
fengmk2 authored Dec 21, 2024
1 parent 42046e3 commit 79bf8af
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Keep track of mocks
export interface MockItem {
obj: any;
key: string;
key: PropertyKey;
descriptor: PropertyDescriptor;
hasOwnProperty: boolean;
}
Expand All @@ -13,7 +13,7 @@ const cache = new Map<any, Set<any>>();
/**
* Mocks a value of an object.
*/
export function mock(obj: any, key: string, value?: any) {
export function mock(obj: any, key: PropertyKey, value?: any) {
const hasOwnProperty = Object.hasOwn(obj, key);
mocks.push({
obj,
Expand Down Expand Up @@ -75,7 +75,7 @@ export function restore() {
cache.clear();
}

export function isMocked(obj: any, key: string) {
export function isMocked(obj: any, key: PropertyKey) {
const flag = cache.get(obj);
return flag ? flag.has(key) : false;
}
25 changes: 23 additions & 2 deletions test/method.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,12 @@ describe('Mock methods', () => {
});

describe('Mock property', () => {
const fooSymbol = Symbol('foo');
const config = {
enableCache: true,
delay: 10,
[fooSymbol]: 'bar',
1: 'one',
};

const plainObj = Object.create(null);
Expand All @@ -89,6 +92,24 @@ describe('Mock property', () => {
assert.equal(plainObj.testKey, 'mockValue', 'testKey is mockValue');
});

it('Should mock Symbol property successfully', () => {
muk(config, fooSymbol, 'mockValue');
assert.equal(config[fooSymbol], 'mockValue', 'fooSymbol is mockValue');
assert.equal(isMocked(config, fooSymbol), true, 'fooSymbol is mocked');
restore();
assert.equal(config[fooSymbol], 'bar', 'fooSymbol is bar');
assert.equal(isMocked(config, fooSymbol), false, 'fooSymbol is not mocked');
});

it('Should mock number property successfully', () => {
muk(config, 1, 'mockValue');
assert.equal(config[1], 'mockValue', '1 is mockValue');
assert.equal(isMocked(config, 1), true, '1 is mocked');
restore();
assert.equal(config[1], 'one', '1 is one');
assert.equal(isMocked(config, 1), false, '1 is not mocked');
});

it('Should alias mock method work', () => {
mock(plainObj, 'testKey', 'mockValue');
assert.equal(plainObj.testKey, 'mockValue', 'testKey is mockValue');
Expand Down Expand Up @@ -116,10 +137,10 @@ describe('Mock property', () => {
muk(process.env, 'HOME', '/mockhome');
muk(config, 'notExistProp', 'value');
muk(process.env, 'notExistProp', 0);
assert.deepEqual(Object.keys(config), [ 'enableCache', 'delay', 'notExistProp' ]);
assert.deepEqual(Object.keys(config), [ '1', 'enableCache', 'delay', 'notExistProp' ]);

restore();
assert.deepEqual(Object.keys(config), [ 'enableCache', 'delay' ]);
assert.deepEqual(Object.keys(config), [ '1', 'enableCache', 'delay' ]);
assert.equal(config.enableCache, true, 'enableCache is true');
assert.equal(config.delay, 10, 'delay is 10');
assert.equal(process.env.HOME, home, 'process.env.HOME is ' + home);
Expand Down

0 comments on commit 79bf8af

Please sign in to comment.