Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: mock property type support symbol and number #3

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading