Skip to content

Commit 1e63442

Browse files
committed
feat: mock property type support symbol and number
1 parent 42046e3 commit 1e63442

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Keep track of mocks
22
export interface MockItem {
33
obj: any;
4-
key: string;
4+
key: PropertyKey;
55
descriptor: PropertyDescriptor;
66
hasOwnProperty: boolean;
77
}
@@ -13,7 +13,7 @@ const cache = new Map<any, Set<any>>();
1313
/**
1414
* Mocks a value of an object.
1515
*/
16-
export function mock(obj: any, key: string, value?: any) {
16+
export function mock(obj: any, key: PropertyKey, value?: any) {
1717
const hasOwnProperty = Object.hasOwn(obj, key);
1818
mocks.push({
1919
obj,
@@ -75,7 +75,7 @@ export function restore() {
7575
cache.clear();
7676
}
7777

78-
export function isMocked(obj: any, key: string) {
78+
export function isMocked(obj: any, key: PropertyKey) {
7979
const flag = cache.get(obj);
8080
return flag ? flag.has(key) : false;
8181
}

test/method.test.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,12 @@ describe('Mock methods', () => {
7272
});
7373

7474
describe('Mock property', () => {
75+
const fooSymbol = Symbol('foo');
7576
const config = {
7677
enableCache: true,
7778
delay: 10,
79+
[fooSymbol]: 'bar',
80+
1: 'one',
7881
};
7982

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

95+
it('Should mock Symbol property successfully', () => {
96+
muk(config, fooSymbol, 'mockValue');
97+
assert.equal(config[fooSymbol], 'mockValue', 'fooSymbol is mockValue');
98+
assert.equal(isMocked(config, fooSymbol), true, 'fooSymbol is mocked');
99+
restore();
100+
assert.equal(config[fooSymbol], 'bar', 'fooSymbol is bar');
101+
assert.equal(isMocked(config, fooSymbol), false, 'fooSymbol is not mocked');
102+
});
103+
104+
it('Should mock number property successfully', () => {
105+
muk(config, 1, 'mockValue');
106+
assert.equal(config[1], 'mockValue', '1 is mockValue');
107+
assert.equal(isMocked(config, 1), true, '1 is mocked');
108+
restore();
109+
assert.equal(config[1], 'one', '1 is one');
110+
assert.equal(isMocked(config, 1), false, '1 is not mocked');
111+
});
112+
92113
it('Should alias mock method work', () => {
93114
mock(plainObj, 'testKey', 'mockValue');
94115
assert.equal(plainObj.testKey, 'mockValue', 'testKey is mockValue');
@@ -116,10 +137,10 @@ describe('Mock property', () => {
116137
muk(process.env, 'HOME', '/mockhome');
117138
muk(config, 'notExistProp', 'value');
118139
muk(process.env, 'notExistProp', 0);
119-
assert.deepEqual(Object.keys(config), [ 'enableCache', 'delay', 'notExistProp' ]);
140+
assert.deepEqual(Object.keys(config), [ '1', 'enableCache', 'delay', 'notExistProp' ]);
120141

121142
restore();
122-
assert.deepEqual(Object.keys(config), [ 'enableCache', 'delay' ]);
143+
assert.deepEqual(Object.keys(config), [ '1', 'enableCache', 'delay' ]);
123144
assert.equal(config.enableCache, true, 'enableCache is true');
124145
assert.equal(config.delay, 10, 'delay is 10');
125146
assert.equal(process.env.HOME, home, 'process.env.HOME is ' + home);

0 commit comments

Comments
 (0)