-
Notifications
You must be signed in to change notification settings - Fork 120
/
assert.js
234 lines (210 loc) · 9.01 KB
/
assert.js
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { RuntimeError } from "error";
import ExpressionParser from "service/ExpressionParser";
import { migrateAssertVisible } from "service/suite";
export function buildAssertionTpl( assertionCall, command, preCode ) {
try {
const cbBody = createCbBody( command );
if ( command.method === "assertConsoleMessage" ) {
return templateConsoleMessageAssertion( cbBody, command.assert, preCode );
}
if ( command.method === "assertDialog" ) {
return templateDialogAssertion( cbBody, command.assert, command.params, preCode );
}
return justify( `${ preCode }\nresult = ${ assertionCall };` ) + ` ${ cbBody }`;
} catch ( err ) {
console.warn( "Renderer process: buildAssertionTpl error:", err, { assertionCall, command, preCode });
throw err;
}
}
export function stringifyTypes( types, unit = "", op = "<" ) {
return Object.keys( types._enabled )
.filter( ( key ) => types._enabled[ key ])
.map( ( el ) => `\`${ el }\` ${ op } \`${ types[ el ] }${ unit }\`` ).join( ", " );
}
function templateConsoleMessageAssertion( cbBody, assert, comment ) {
return justify( `
${ comment }
result = consoleLog
.filter( message => ( "${ assert.type }" === "any" || message.type() === "${ assert.type }" ) )
.map( message => message.text() );
${ cbBody.replace( /^ /g, "" ) }
${ assert.reset !== false ? `consoleLog = [];` : `` }
` );
}
function templateDialogAssertion( cbBody, assert, params, comment ) {
return justify( `
${ comment }
result = dialogLog
.filter( dialog => ( "${ assert.type }" === "any" || dialog.type() === "${ assert.type }" ) )
.map( dialog => dialog.message() );
${ cbBody.replace( /^ /g, "" ) }
${ assert.reset !== false ? `dialogLog = [];` : `` }
` );
}
/**
* Some assertions allow you to enable/disable params
* In such case options._enabled map has information about what of params are enabled
* { key1: boolean }
*
* @param {Object} options
* @returns {Object}
*/
function getEnabledOptions( options ) {
return Object.entries( options._enabled )
.filter( pair => Boolean( pair[ 1 ]) )
.reduce( ( carry, pair ) => {
const key = pair[ 0 ];
carry[ key ] = options[ key ];
return carry;
}, {});
}
/**
*
* @param {String} text
* @returns {String}
*/
export function justify( text ) {
return ( "\n" + text ).split( "\n" ).map( line => " " + line ).join( "\n" );
}
function parseTpl( value, id, type ) {
// https://github.com/dsheiko/puppetry/pull/91/commits/8c939d2bd822a88eb49511e78ac5055d17828962
if ( typeof type === "undefined" || !( type == "string" || type == "text" ) ) {
return JSON.stringify( value );
}
// @see ./src/component/Schema/Params/Element/assertText.js
if ( typeof type !== "undefined" && type === "text" ) {
value = value.replace( /\n+/gm, "\n" );
}
const parser = new ExpressionParser( id );
return parser.stringify( value );
}
function negate( not ) {
// can be boolean, but now we have in AssertConsoleMessage "true"/"false"
return ( not === "false" || !not ) ? `` : `.not`;
}
function getExpectation( method, params, assert ) {
switch ( method ) {
case "assertConsoleMessage":
return JSON.stringify( `to find console messages `
+ `of type "${ assert.type }" with text ${ assert.assertion !== "haveString" ? "~ " : "" }`
+ `"${ assert.value }"` );
case "assertDialog":
return JSON.stringify( `to find dialogs `
+ `of type "${ assert.type }" with message ${ assert.assertion !== "haveString" ? "~ " : "" }`
+ `"${ assert.value }"` );
case "assertTextCount":
case "assertNodeCount":
return JSON.stringify( `number of the matching elements` );
case "assertScroll":
return typeof params.direction !== "undefined"
? JSON.stringify( `scroll offset ${ params.direction }` ) : `null`;
default:
return "null";
}
}
function createCbBody({ assert, target, method, id, params }) {
const { assertion, value, operator, position, not, ...options } = assert,
source = `${ target }.${ method }`;
switch ( assertion ) {
case "visible":
return justify( `expect( result ).toBeVisible( ${ JSON.stringify( assert ) }, "${ source }" );` );
case "screenshot":
return justify( `expect( result ).toMatchScreenshot( ${ options.mismatchTolerance }, "${ source }" );` );
case "selector":
return justify( `expect( result ).toMatchSelector( "${ value }", "${ source }" );` );
case "boolean":
// support Puppetry < 3.0.0
if ( method === "assertVisible" ) {
return justify( `expect( result ).toBeVisible( ${ JSON.stringify(
migrateAssertVisible({ method, assert }) ) }, "${ source }" );` );
}
return justify( `expect( result )${ value ? "" : ".not" }.toBeOk( "${ source }" );` );
case "number":
return justify( `expect( result ).toPassCondition( "${ operator }", ${ value },`
+ ` ${ getExpectation( method, params, assert ) }, "${ source }" );` );
case "contains":
return justify( `expect( result ).toIncludeSubstring( ${ parseTpl( value, id, options.assertionType ) }`
+ `, "${ source }" );` );
case "!contains":
return justify( `expect( result ).not.toIncludeSubstring( ${ parseTpl( value, id, options.assertionType ) }`
+ `, "${ source }" );` );
case "equals":
return justify( `expect( result ).toBeEqual( ${ parseTpl( value, id, options.assertionType ) }, "${ source }" );` );
case "!equals":
return justify( `expect( result )`
+ `.not.toBeEqual( ${ parseTpl( value, id, options.assertionType ) }, "${ source }" );` );
case "empty":
return justify( `expect( result ).toBeEmpty( "${ source }" );` );
case "!empty":
return justify( `expect( result ).not.toBeEmpty( "${ source }" );` );
case "hasClass":
return justify( `expect( result ).toHaveClass( "${ params.name }", "${ source }" );` );
case "!hasClass":
return justify( `expect( result ).not.toHaveClass( "${ params.name }", "${ source }" );` );
case "hasAttribute":
return justify( `expect( result ).toHaveAttribute( "${ params.name }", "${ source }" );` );
case "!hasAttribute":
return justify( `expect( result ).not.toHaveAttribute( "${ params.name }", "${ source }" );` );
case "hasProperty":
return justify( `expect( result ).toHavePropertyTrue( "${ params.name }", "${ source }" );` );
case "!hasProperty":
return justify( `expect( result ).not.toHavePropertyTrue( "${ params.name }", "${ source }" );` );
case "haveString":
return justify( `expect( result )${ negate( not ) }`
+ `.toHaveString( ${ parseTpl( value, id, options.assertionType ) }`
+ `, ${ getExpectation( method, params, assert ) }, "${ source }" );` );
case "haveSubstring":
return justify( `expect( result )${ negate( not ) }`
+ `.toHaveSubstring( ${ parseTpl( value, id, options.assertionType ) }`
+ `, ${ getExpectation( method, params, assert ) }, "${ source }" );` );
case "position":
return justify( `expect( result ).toMatchPosition`
+ `( "${ position }", "${ target }", "${ options.target }", "${ source }" );` );
case "boundingBox":
return justify( `expect( result )`
+ `.toMatchBoundingBoxSnapshot( ${ JSON.stringify( options, null, " " ) }, "${ source }" );` );
case "response":
return justify( `expect( result )`
+ `.toMatchResponse( ${ JSON.stringify({ ...options, not }, null, " " ) }, "${ source }" );` );
case "rest":
return justify( `
result.data = await result.text();
ENV[ "PUPPETRY_LAST_RESPONSE_TEXT" ] = result.data;
expect( result )`
+ `.toMatchRest( ${ JSON.stringify( params.url ) }, `
+ `${ JSON.stringify( options, null, " " ) }, "${ source }" );` );
case "assertPerformanceTiming":
return resolveTimingAssertion( options, source );
case "assertAssetWeight":
return resolveAssetAssertion( "toMatchAssetWeight", options, source );
case "assertAssetCount":
return resolveAssetAssertion( "toMatchAssetCount", options, source );
case "assertGaTracking":
return justify( `expect( result )`
+ `.toMatchGaTracking( ${ JSON.stringify( options, null, " " ) }, "${ source }" );` );
default:
throw RuntimeError( `Invalid assertion '${ assertion }'` );
}
}
function resolveTimingAssertion( options, source ) {
const data = getEnabledOptions( options );
return Object.entries( data ).reduce( ( carry, pair ) => {
const type = JSON.stringify( pair[ 0 ]),
rawVal = parseInt( pair[ 1 ], 10 ),
val = ( isNaN( rawVal ) ? 0 : rawVal );
carry += justify( `expect( result ).toMatchTiming( `
+ `${ type }, ${ val }, "${ source }" ); ` );
return carry;
}, "" );
}
function resolveAssetAssertion( assertionMethod, options, source ) {
const data = getEnabledOptions( options );
return Object.entries( data ).reduce( ( carry, pair ) => {
const type = JSON.stringify( pair[ 0 ]),
rawVal = parseInt( pair[ 1 ], 10 ),
val = ( isNaN( rawVal ) ? 0 : rawVal ) * ( assertionMethod === "toMatchAssetCount" ? 1 : 1000 );
carry += justify( `expect( result ).${ assertionMethod }( `
+ `${ type }, ${ val }, "${ source }" ); ` );
return carry;
}, "" );
}