forked from css-modules/css-modules-loader-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-cases.js
55 lines (50 loc) · 2.19 KB
/
test-cases.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
'use strict';
import assert from 'assert'
import fs from 'fs'
import path from 'path'
import FileSystemLoader from '../src/file-system-loader'
let normalize = ( str ) => {
return str.replace( /\r\n?/g, '\n' );
}
const pipelines = {
'test-cases': undefined,
'cssi': []
}
Object.keys( pipelines ).forEach( dirname => {
describe( dirname, () => {
let testDir = path.join( __dirname, dirname )
fs.readdirSync( testDir ).forEach( testCase => {
if ( fs.existsSync( path.join( testDir, testCase, 'source.css' ) ) ) {
it( 'should ' + testCase.replace( /-/g, ' ' ), done => {
let expected = normalize( fs.readFileSync( path.join( testDir, testCase, 'expected.css' ), 'utf-8' ) )
let loader = new FileSystemLoader( testDir, pipelines[dirname] )
let expectedTokens = JSON.parse( fs.readFileSync( path.join( testDir, testCase, 'expected.json' ), 'utf-8' ) )
loader.fetch( `${testCase}/source.css`, '/' ).then( tokens => {
assert.equal( loader.finalSource, expected )
assert.equal( JSON.stringify( tokens ), JSON.stringify( expectedTokens ) )
} ).then( done, done )
} );
}
} );
} );
} )
// special case for testing multiple sources
describe( 'multiple sources', () => {
let testDir = path.join( __dirname, 'test-cases' )
let testCase = 'multiple-sources';
let dirname = 'test-cases';
if ( fs.existsSync( path.join( testDir, testCase, 'source1.css' ) ) ) {
it( 'should ' + testCase.replace( /-/g, ' ' ), done => {
let expected = normalize( fs.readFileSync( path.join( testDir, testCase, 'expected.css' ), 'utf-8' ) )
let loader = new FileSystemLoader( testDir, pipelines[dirname] )
let expectedTokens = JSON.parse( fs.readFileSync( path.join( testDir, testCase, 'expected.json' ), 'utf-8' ) )
loader.fetch( `${testCase}/source1.css`, '/' ).then( tokens1 => {
loader.fetch( `${testCase}/source2.css`, '/' ).then( tokens2 => {
assert.equal( loader.finalSource, expected )
const tokens = Object.assign({}, tokens1, tokens2);
assert.equal( JSON.stringify( tokens ), JSON.stringify( expectedTokens ) )
} ).then( done, done )
})
} );
}
} );