Class
The @archibald/testing package provides functionality to work with classes.
mockClass
mockClass function can be used to mock all methods on a class.
import { mockClass } from '@archibald/testing';
mockClass(CLASS, RESPONSE);
Example
import { mockClass } from '@archibald/testing';
const propOneValue = 'propOne';
const propTwoValue = 'propTwo';
const newValue = 'new value';
class TestClass {
methodOne() {
return propOneValue;
}
methodTwo() {
return propTwoValue;
}
}
describe('mockClass', () => {
it('should mock all methods of a class', () => {
const testClassInstance = new TestClass();
expect(testClassInstance.methodOne()).toBe(propOneValue);
expect(testClassInstance.methodTwo()).toBe(propTwoValue);
mockClass(TestClass, newValue);
expect(testClassInstance.methodOne()).toBe(newValue);
expect(testClassInstance.methodTwo()).toBe(newValue);
});
});
mockMethod
mockMethod function can be used to mock all methods on a class.
import { mockMethod } from '@archibald/testing';
mockMethod(CLASS, METHOD, RESPONSE);
Example
import { mockMethod } from '@archibald/testing';
const propOneValue = 'propOne';
const propTwoValue = 'propTwo';
const newValue = 'new value';
class TestClass {
methodOne() {
return propOneValue;
}
methodTwo() {
return propTwoValue;
}
}
describe('mockMethod', () => {
it('should mock one method of a class', () => {
const testClassInstanceOne = new TestClass();
expect(testClassInstanceOne.methodOne()).toBe(propOneValue);
expect(testClassInstanceOne.methodTwo()).toBe(propTwoValue);
mockMethod(TestClass, 'methodOne', newValue);
expect(testClassInstanceOne.methodOne()).toBe(newValue);
expect(testClassInstanceOne.methodTwo()).toBe(propTwoValue);
});
});