summaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/cypress/support/jsonBuilders/jsonBuilder.ts
blob: 0bab4e1dafda4a1f1ef6a3e79e446db81bedb9a3 (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
export class JsonBuilder<T> implements IJsonBuilder<T>{
  currentValue: T;

  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) {
    cy.readFile(jsonPath).then((res) => {
      this.basicJson(res, url, 200, 0, url,);
    })
  }
}

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;
}