-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStageDesign.ts
138 lines (118 loc) · 5.66 KB
/
StageDesign.ts
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
'use strict';
import * as cheerio from 'cheerio';
import * as fs from 'fs';
import { StageManager } from "serenity-js/lib/serenity/stage";
import { DomainEvent } from "serenity-js/lib/serenity/domain";
import { SetDesigner } from "./SetDesigner";
import { StageDesignChanging } from "../support/StageDesignChanging";
export class StageDesign {
private static instance: StageDesign;
private resolvePromise: any;
static create(): StageDesign{
if (!StageDesign.instance) {
StageDesign.instance = new StageDesign();
}
return StageDesign.instance;
}
private constructor() {
this.parameters = [];
}
private stageManager: StageManager;
private instr: string;
setStageManager(stageManager: StageManager) {
this.stageManager = stageManager;
}
get instructions(): string {
return this.instr;
}
set instructions(newInstructions: string) {
this.instr = newInstructions;
if (this.instr) {
this.stageManager.notifyOf(new StageDesignChanging(this, Date.now()));
}
}
build(): SetDesigner {
return new SetDesigner();
}
parameters: string[];
private cast: any;
stageReady(): void {
this.resolvePromise();
}
toIntroduceCast(users: any): void {
const originalIndexHtml = fs.readFileSync('./index.html', 'utf8');
this.cast = users.map(user => {
user.roles = [user.roles];
return user;
});
const responseBody = JSON.stringify(this.cast);
const $ = cheerio.load(originalIndexHtml);
const castScript = ' <script id="cast">\n' +
' window.mockBackend.respondWith(/\\/web\\/api\\/masterData\\/planners\\//, [\n' +
' 200,\n' +
` { "Content-Language": "de-DE", "Content-Type": "application/json", "Content-Length": ${responseBody.length} },\n` +
` '${responseBody}'\n` +
' ]);\n' +
' </script>\n';
$(castScript).insertAfter('#mockbackend');
fs.writeFileSync('./index.html', $.html(), { encoding: 'utf8' });
}
toAnnounceActor(userName: string): void {
const indexHtml = fs.readFileSync('./index.html', 'utf8');
const responseBody = JSON.stringify(this.cast.find(u => u.name === userName));
const $ = cheerio.load(indexHtml);
const script = '<script id="user">' +
' window.mockBackend.respondWith(/\\/web\\/api\\/user/, [\n' +
' 200,\n' +
` { "Content-Language": "de-DE", "Content-Type": "application/json", "Content-Length": ${responseBody.length} },\n` +
` '${responseBody}'\n` +
' ]);\n' +
' </script>\n';
$(script).insertAfter('#cast');
fs.writeFileSync('./index.html', $.html(), { encoding: 'utf8' });
}
forANewJoiningSequence(): PromiseLike<void> {
return new Promise <void>(resolve => {
this.resolvePromise = resolve;
this.parameters.push(fs.readFileSync('./testui/testData/newJS_PlantNameOnly.json', 'utf-8'));
this.instructions = 'var callback = arguments[arguments.length-1];\n' +
this.POST_A_NEW_JS +
this.GET_A_JS +
this.AUTOSAVE_A_JS +
this.TURN_ON_EDIT_MODE +
'\ncallback();';
});
}
private POST_A_NEW_JS = 'window.mockBackend.respondWith("POST", /\\/web\\/api\\/joiningSequences\\/$/, function (xhr) {\n' +
' var joiningSequenceDto = JSON.parse(xhr.requestBody);\n' +
' xhr.respond(200, { "Content-Language": "de-DE" }, "");\n' +
'});\n'
private AUTOSAVE_A_JS = 'window.mockBackend.respondWith("PUT", /\\/web\\/api\\/joiningSequences\\/([\\w,-]*)/, [\n' +
' 200,\n' +
' { "Content-Type": "application/json", "Content-Language": "de-DE", "Content-Length": 34 },\n' +
' \'{"errorCode":0,"status":"success"}\'\n' +
']);'
private GET_A_JS = 'window.mockBackend.respondWith("GET", /\\/web\\/api\\/joiningSequences\\/([\\w,-]*)$/, function (xhr, id) {\n' +
' var joiningSequenceDto = JSON.parse(arguments[1]);\n' +
' var now = Date.now();\n' +
' joiningSequenceDto.joiningSequence.created = now;\n' +
' joiningSequenceDto.joiningSequence.modified = now;\n' +
' joiningSequenceDto.joiningSequence.uuid = id;\n' +
' var body = JSON.stringify(joiningSequenceDto, function (key, value) {\n' +
' if (key === "jsUuid") {\n' +
' return id;\n' +
' } else {\n' +
' return value;\n' +
' }\n' +
' });\n' +
' xhr.respond(200,\n' +
' { "Content-Type": "application/json", "Content-Language": "de-DE", "Content-Length": body.length },\n' +
' body\n' +
' );\n' +
'});\n'
private TURN_ON_EDIT_MODE = 'window.mockBackend.respondWith("POST", /\\/web\\/api\\/joiningSequences\\/([\\w,-]*)\\/writeLock$/, [\n' +
' 200,\n' +
' { "Content-Type": "application/json", "Content-Language": "de-DE", "Content-Length": 65 },\n' +
' \'{"token":"f41fab4a-ae1a-4965-894e-19434fdc452f","user":"Planner"}\'\n' +
']);\n'
}