-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
createTestCase.js
119 lines (104 loc) · 4.63 KB
/
createTestCase.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
/* global Q */
/* exported createTestCase */
/**
* Creates a single test case based on the options provided. It uses files located in `../_fixtures/` directory to build
* a proper assertions. The input file should be located in `../fixtures/options.name/options.wordVersion/options.browser.html`,
* and the expected output in `../_fixtures/options.name/expected.html`. If the expected output is different for the given
* browser (`options.browser`) than in the most cases the separate file can be used - it should be located
* under `../_fixtures/options.name/options.wordVersion/expected_options.browser.html`.
*
* @param {Object} options
* @param {String} options.name Fixture name.
* @param {String} options.wordVersion Fixture word version.
* @param {String} options.browser Browser name.
* @param {Boolean} [options.compareRawData=false] If `true` test case will assert against raw paste's `data.dataValue` rather than
* what will appear in the editor after all transformations and filtering.
* @param {Array} [options.customFilters] Array of custom filters (like [ pfwTools.filters.font ]) which will be used during assertions.
* @returns {Function}
*/
function createTestCase( options ) {
return function() {
var inputPathHtml = [ '_fixtures', options.name, options.wordVersion, options.browser ].join( '/' ) + '.html',
inputPathRtf = [ '_fixtures', options.name, options.wordVersion, options.browser ].join( '/' ) + '.rtf',
outputPath = [ '_fixtures', options.name, '/expected.html' ].join( '/' ),
specialCasePath = [ '_fixtures', options.name, options.wordVersion, 'expected_' + options.browser ].join( '/' ) + '.html',
deCasher = '?' + Math.random().toString( 36 ).replace( /^../, '' ), // Used to trick the browser into not caching the html files.
editor = this.editor,
load = function( path ) {
assert.isObject( CKEDITOR.ajax, 'Ajax plugin is required' );
var deferred = Q.defer();
CKEDITOR.ajax.load( path, function( data ) {
deferred.resolve( data );
} );
return deferred.promise;
};
Q.all( [
load( inputPathHtml + deCasher ),
load( inputPathRtf + deCasher ),
load( outputPath + deCasher ),
load( specialCasePath + deCasher )
] ).done( function( values ) {
var inputFixtureHtml = values[ 0 ],
inputFixtureRtf = values[ 1 ],
// If browser-customized expected result was found, use it. Otherwise go with the regular expected.
expectedValue = values[ 3 ] !== null ? values[ 3 ] : values[ 2 ],
that = this;
// Both null means that fixture file was not found - skipping test.
if ( inputFixtureHtml === null && inputFixtureRtf === null ) {
resume( function() {
assert.ignore();
} );
return;
}
// Single null means that one of 2 required files is missing.
else if ( inputFixtureHtml === null || inputFixtureRtf === null ) {
resume( function() {
assert.isNotNull( inputFixtureHtml, '"' + inputPathHtml + '" file is missing' );
assert.isNotNull( inputFixtureRtf, '"' + inputPathRtf + '" file is missing' );
} );
}
editor.once( 'paste', function( evt ) {
// Clipboard strips white spaces from pasted content if those are not encoded.
// This is **needed only for non-IE/Edge fixtures**, as these browsers doesn't encode nbsp char on it's own.
if ( CKEDITOR.env.ie && CKEDITOR.tools.array.indexOf( [ 'chrome', 'firefox', 'safari' ], options.browser ) !== -1 ) {
var encodedData;
/* jshint ignore:start */
encodedData = evt.data.dataValue.replace( / /g, ' ' );
/* jshint ignore:end */
evt.data.dataValue = encodedData;
}
}, null, null, 5 );
assert.isNotNull( expectedValue, '"expected.html" missing.' );
// Tu wklejam
var nativeDataTransfer = bender.tools.mockNativeDataTransfer(),
dataTransfer,
eventData = {};
if ( CKEDITOR.plugins.clipboard.isCustomDataTypesSupported ) {
nativeDataTransfer.setData( 'text/html', inputFixtureHtml );
nativeDataTransfer.setData( 'text/rtf', inputFixtureRtf );
}
dataTransfer = new CKEDITOR.plugins.clipboard.dataTransfer( nativeDataTransfer );
eventData.dataTransfer = dataTransfer;
eventData.dataValue = inputFixtureHtml;
if ( !eventData.type ) {
eventData.type = 'auto';
}
eventData.method = 'paste';
editor.once( 'pasteFromWordImage', function() {
setTimeout( function() {
resume( function() {
assert.beautified.html( expectedValue, editor.getData(), {
fixStyles: true,
sortAttributes: true,
customFilters: options.customFilters
} );
} );
}.bind( that ), 5 );
} );
editor.setData( '', function() {
editor.fire( 'paste', eventData );
} );
} );
wait();
};
}