I'm currently trying to write some test with jest (newbie). Regarding to my latest post, I've got the following situation:
I've got multiple price fields. Each price field has its own renderer. A renderer has a reference to its pricefield. A pricefield can determine some html snippet from the dom.
// priceField.js
class PriceField {
constructor() {
const renderer = new PriceFieldRenderer(this);
// do something with renderer
}
/**
* Determines Node in DOM for this price field
*/
determinePriceFieldNode() {
return document.querySelector(".price-field");
}
}
export default PriceField;
// priceFieldRenderer.js
class PriceFieldRenderer {
constructor(priceField) {
this.priceFieldNode = priceField.determinePriceFieldNode();
}
}
export default PriceFieldRenderer;
Now I want to test PriceFieldRenderer
(with jest). Therefore, I need to mock PriceField
and its determinePriceFieldNode()
function.
However, I'm not able to pass a mocked price field instance as constructor argument. I've read the official documentation with its sound-player example and googled about 2 hours. Also i tested different implementations, but I'm not able to solve it. Here is one code snippet which throws following error: TypeError: priceField.determinePriceFieldNode is not a function
jest.mock("./priceField");
import PriceFieldRenderer from "./priceFieldRenderer";
import PriceField from "./priceField";
describe("Price field renderer", () => {
test("with calculated price", () => {
const priceField = PriceField.mockImplementation(() => {
return {
determinePriceFieldNode: () => "<html/>"
};
});
const sut = new PriceFieldRenderer(priceField);
expect(sut).toBeDefined();
});
});
I'm sure the way I did is not the best and there is some help out there :)