aboutsummaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/cypress/support/jsonBuilders/jsonBuilder.ts
blob: 2583d46a148c4dabf34b181fcc303039c56b8abf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
export class JsonBuilder<T> implements IJsonBuilder<T>{
  currentValue?: T;

  constructor(currentValue ?: T){
    this.currentValue = currentValue;
  }
  public basicJson(json: JSON, url: string, status: number, delay: number, alias: string, changeResFunc?: Function) : void {
    this.currentValue = <T>JSON.parse(JSON.stringify(json));
    this.currentValue = changeResFunc ? changeResFunc(this.currentValue) : this.currentValue;
    return this.initMockCall(url, status, delay, alias);
  }

  public initMockCall(url: string, status: number, delay: number, alias: string) {
      cy.server()
        .route({
          method: 'GET',
          status: status,
          delay : delay ? delay : 0,
          url: url,
          response: JSON.stringify(this.currentValue)
        }).as(alias);
  }
  public basicMock(jsonPath: string, url: string ,changeResFunc?: Function) {
    cy.readFile(jsonPath).then((res) => {
      this.basicJson(res, url, 200, 0, url, changeResFunc);
    })
  }
}

export interface IJsonBuilder<T>{
  basicJson(json: JSON, url: string, status: number, delay: number, alias: string, changeResFunc?: Function) : void;
  initMockCall(url: string, status: number, delay: number, alias: string): void;
  basicMock(jsonPath: string, url: string): void;
}