Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 1 | export class JsonBuilder<T> implements IJsonBuilder<T>{ |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 2 | currentValue?: T; |
Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 3 | |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 4 | constructor(currentValue ?: T){ |
| 5 | this.currentValue = currentValue; |
| 6 | } |
Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 7 | 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 Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 23 | public basicMock(jsonPath: string, url: string ,changeResFunc?: Function) { |
Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 24 | cy.readFile(jsonPath).then((res) => { |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 25 | this.basicJson(res, url, 200, 0, url, changeResFunc); |
Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 26 | }) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | export 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 | } |