Skip to content

Commit

Permalink
Add tests with readonly fields (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
EmandM authored Aug 15, 2023
1 parent 02db125 commit 5fbd2c0
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 22 deletions.
40 changes: 20 additions & 20 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@
"tslint": "^5.20.1",
"tslint-config-airbnb": "^5.11.2",
"tslint-no-unused-expression-chai": "^0.1.4",
"typescript": "^5.0.4"
"typescript": "^5.1.6"
}
}
7 changes: 7 additions & 0 deletions test/resources/classes/class-with-readonly.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class TestReadonlyClass {
private readonly config: { [p: string]: any };

public getConfig = (): { [p: string]: any } => {
return this.config;
}
}
1 change: 1 addition & 0 deletions test/resources/consumers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './function-consumer';
export * from './other-consumer';
export * from './broken-test-class-consumer';
export * from './broken-default-class-consumer';
export * from './readonly-class-consumer';
12 changes: 12 additions & 0 deletions test/resources/consumers/readonly-class-consumer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { TestReadonlyClass } from '../classes/class-with-readonly';

export class ReadonlyClassConsumer {
private readonlyClass: TestReadonlyClass;
constructor() {
this.readonlyClass = new TestReadonlyClass();
}

public getConfig(): { [val: string]: any } {
return this.readonlyClass.getConfig();
}
}
14 changes: 13 additions & 1 deletion test/spec/class-mock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { MockManager } from '../../src/managers';
import * as staticTestClass from '../resources/classes/static-test-class';
import * as testClass from '../resources/classes/test-class';
import * as defaultClass from '../resources/classes/test-default-export';
import { DefaultClassConsumer, StaticTestClassConsumer, TestClassConsumer } from '../resources/consumers';
import * as readonlyClass from '../resources/classes/class-with-readonly';
import { DefaultClassConsumer, StaticTestClassConsumer, TestClassConsumer, ReadonlyClassConsumer } from '../resources/consumers';

describe('Class Mock', () => {
describe('Mock Class', () => {
Expand All @@ -26,6 +27,17 @@ describe('Class Mock', () => {
manager.restore();
});

it('should work with readonly fields', () => {
const manager = ImportMock.mockClass(readonlyClass, 'TestReadonlyClass');
const mockValue = {configValue: 'test-config'};

manager.mock('getConfig', mockValue)
const consumer = new ReadonlyClassConsumer();
expect(consumer.getConfig()).to.equal(mockValue);

manager.restore();
})

});

describe('Mock Static Class', () => {
Expand Down

0 comments on commit 5fbd2c0

Please sign in to comment.