aboutsummaryrefslogtreecommitdiffstats
path: root/tests/runner/listeners.js
blob: ed6fb24e8dfbfb9cf733160d0f26185d9cbaa4d5 (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
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
( function() {
	"use strict";

	// Get the report ID from the URL.
	var match = location.search.match( /reportId=([^&]+)/ );
	if ( !match ) {
		return;
	}
	var id = match[ 1 ];

	// Adopted from https://github.com/douglascrockford/JSON-js
	// Support: IE 11+
	// Using the replacer argument of JSON.stringify in IE has issues
	// TODO: Replace this with a circular replacer + JSON.stringify + WeakSet
	function decycle( object ) {
		var objects = [];

		// The derez function recurses through the object, producing the deep copy.
		function derez( value ) {
			if (
				typeof value === "object" &&
				value !== null &&
				!( value instanceof Boolean ) &&
				!( value instanceof Date ) &&
				!( value instanceof Number ) &&
				!( value instanceof RegExp ) &&
				!( value instanceof String )
			) {

				// Return a string early for elements
				if ( value.nodeType ) {
					return value.toString();
				}

				if ( objects.indexOf( value ) > -1 ) {
					return;
				}

				objects.push( value );

				if ( Array.isArray( value ) ) {

					// If it is an array, replicate the array.
					return value.map( derez );
				} else {

					// If it is an object, replicate the object.
					var nu = Object.create( null );
					Object.keys( value ).forEach( function( name ) {
						nu[ name ] = derez( value[ name ] );
					} );
					return nu;
				}
			}

			// Serialize Symbols as string representations so they are
			// sent over the wire after being stringified.
			if ( typeof value === "symbol" ) {

				// We can *describe* unique symbols, but note that their identity
				// (e.g., `Symbol() !== Symbol()`) is lost
				var ctor = Symbol.keyFor( value ) !== undefined ? "Symbol.for" : "Symbol";
				return ctor + "(" + JSON.stringify( value.description ) + ")";
			}

			return value;
		}
		return derez( object );
	}

	function send( type, data ) {
		var json = JSON.stringify( {
			id: id,
			type: type,
			data: data ? decycle( data ) : undefined
		} );
		var request = new XMLHttpRequest();
		request.open( "POST", "/api/report", true );
		request.setRequestHeader( "Content-Type", "application/json" );
		request.send( json );
		return request;
	}

	require( [ "qunit" ], function( QUnit ) {

		// Send acknowledgement to the server.
		send( "ack" );

		QUnit.on( "testEnd", function( data ) {
			send( "testEnd", data );
		} );

		QUnit.on( "runEnd", function( data ) {

			// Reduce the payload size.
			// childSuites is large and unused.
			data.childSuites = undefined;

			var request = send( "runEnd", data );
			request.onload = function() {
				if ( request.status === 200 && request.responseText ) {
					try {
						var json = JSON.parse( request.responseText );
						window.location = json.url;
					} catch ( e ) {
						console.error( e );
					}
				}
			};
		} );
	} );
} )();