/
test-usemin.js
65 lines (58 loc) · 2.67 KB
/
test-usemin.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
/*global describe, before, after, beforeEach, afterEach, describe, it */
var fs = require('fs');
var path = require('path');
var grunt = require('grunt');
var assert = require('assert');
var helpers = require('./helpers');
var usemin = require('../tasks/usemin.js');
var opts = grunt.cli.options;
opts.redirect = !opts.silent;
// XXX Conform to coding guidelines, mostly literral spacing stuff
describe('usemin', function() {
before(helpers.directory('.test'));
describe('replace helper', function() {
it("should take into account path", function() {
usemin.call(grunt,grunt);
// Let's prepare our context: in index.html we do have references
// to images/test.png and images/misc/test.png.
// Usemin's replace is supposed to change this by files
// found on the filesystem matching the same path, but
// potentially prefixed
// e.g images/test.pmg -> images/misc/23012.test.png
grunt.file.mkdir('images');
grunt.file.mkdir('images/misc');
grunt.file.write('images/23012.test.png', "foo");
grunt.file.write('images/misc/2a436.test.png', "foo");
// Let's avoid cluttering the output
grunt.log.muted = true;
var content = grunt.file.read(path.join(__dirname,"fixtures/usemin.html"));
var changed = grunt.helper('replace',content, /<img[^\>]+src=['"]([^"']+)["']/gm);
// Check replace has performed its duty
assert.ok( changed.match(/img[^\>]+src=['"]images\/23012\.test\.png["']/) );
assert.ok( changed.match(/img[^\>]+src=['"]images\/misc\/2a436\.test\.png["']/) );
});
});
describe('usemin:css', function() {
it("should replace a block with link on furnished target", function() {
grunt.log.muted = true;
var block = " foo\nbar\nbaz";
var content = "before block\n" + block + "\nafter block";
var awaited = "before block\n <link rel=\"stylesheet\" href=\"foo\"/>\nafter block";
var changed = grunt.helper('usemin:css', content, block, 'foo');
assert.ok( changed == awaited );
});
});
describe('usemin:post:html', function() {
it('do not depend on tag position', function() {
usemin.call(grunt,grunt);
grunt.log.muted = true;
grunt.file.mkdir('css');
grunt.file.write('css/23012.main.css', "foo");
var content = '<link href="css/main.css" rel="stylesheet"/><link rel="stylesheet" href="css/main.css"/>';
var awaited = '<link href="css/23012.main.css" rel="stylesheet"/><link rel="stylesheet" href="css/23012.main.css"/>';
var changed = grunt.helper('usemin:post:html', content);
grunt.log.writeln("FRED: ["+content+"]["+changed+"]");
assert.ok( changed == awaited );
});
});
});