aboutsummaryrefslogtreecommitdiffstats
path: root/test/runner/listeners.js
blob: a3c52c21e83a4e974bce39b39a909bde8c9adc41 (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
( 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;
				}
			}
			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 );
	}

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

		send( "runEnd", data );
	} );
} )();