blob: 2583d46a148c4dabf34b181fcc303039c56b8abf [file] [log] [blame]
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +03001export class JsonBuilder<T> implements IJsonBuilder<T>{
Ittay Stern6f900cc2018-08-29 17:01:32 +03002 currentValue?: T;
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +03003
Ittay Stern6f900cc2018-08-29 17:01:32 +03004 constructor(currentValue ?: T){
5 this.currentValue = currentValue;
6 }
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +03007 public basicJson(json: JSON, url: string, status: number, delay: number, alias: string, changeResFunc?: Function) : void {
8 this.currentValue = <T>JSON.parse(JSON.stringify(json));
9 this.currentValue = changeResFunc ? changeResFunc(this.currentValue) : this.currentValue;
10 return this.initMockCall(url, status, delay, alias);
11 }
12
13 public initMockCall(url: string, status: number, delay: number, alias: string) {
14 cy.server()
15 .route({
16 method: 'GET',
17 status: status,
18 delay : delay ? delay : 0,
19 url: url,
20 response: JSON.stringify(this.currentValue)
21 }).as(alias);
22 }
Ittay Stern6f900cc2018-08-29 17:01:32 +030023 public basicMock(jsonPath: string, url: string ,changeResFunc?: Function) {
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030024 cy.readFile(jsonPath).then((res) => {
Ittay Stern6f900cc2018-08-29 17:01:32 +030025 this.basicJson(res, url, 200, 0, url, changeResFunc);
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030026 })
27 }
28}
29
30export interface IJsonBuilder<T>{
31 basicJson(json: JSON, url: string, status: number, delay: number, alias: string, changeResFunc?: Function) : void;
32 initMockCall(url: string, status: number, delay: number, alias: string): void;
33 basicMock(jsonPath: string, url: string): void;
34}