We want test the process fuction but we can't its is a private function
widget-a/index.js
import {DataItem} from "models"
export class WidgetA {
data:DataItem[]
constructor(data:DataItem[]){
this.data=data;
}
private render(){
let data:DataItem=process();
// do you render logic here
}
private process(){
// processing algorithm (complex or not)
return somedata_after_process;
}
}
- do not test it -- we get a contract only to public interface
- make it public -- this way we can access ot them
- externalize it to separate funcion -- we can test it and use as a utility function in class
it's easy and extrimly dangerous way we will pay for it later
This kill encapsulation, everything can be used outside your class which not be your real intent. this is can cause to unxepected result in latter
in this way we have a ability to test function and we do not break the code design it's sound safe way to do
widget-a/index.js
import {DataItem} from "models"
import {processor} from "./processor"
export class WidgetA {
data:DataItem[]
constructor(data:DataItem[]){
this.data=data;
}
private render(){
let data:DataItem=process();
// do you render logic here
}
private process(){
return processor(this.data);
}
}
widget-a/processor.js
import {DataItem} from "models";
export function processor (data:DataItem[]){
// processing algorithm (complex or not)
return somedata_after_process;
}