|
| 1 | +/* eslint no-undef: 0 */ |
| 2 | +/* eslint no-console: 0 */ |
| 3 | +"use strict"; |
| 4 | +jest.mock('fs'); |
| 5 | +const fs = require('fs'); |
| 6 | +const path = require('path'); |
| 7 | +const root = process.cwd(); |
| 8 | + |
| 9 | +jest.mock('postcss-import/lib/resolve-id.js', function() { |
| 10 | + const path = require('path'); |
| 11 | + const resolveEngine = { |
| 12 | + resolve: function(id, base, config) { |
| 13 | + return Promise.resolve(path.resolve(base, id)); |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + function resolveId(id, base, config) { |
| 18 | + return resolveEngine.resolve(id, base, config); |
| 19 | + } |
| 20 | + |
| 21 | + resolveId.setResolver = function(fn) { |
| 22 | + resolveEngine.resolve = fn; |
| 23 | + } |
| 24 | + |
| 25 | + return resolveId; |
| 26 | +}); |
| 27 | + |
| 28 | +var sub = require('../index.js').sub; |
| 29 | + |
| 30 | +function mockRender(config) { |
| 31 | + return function(id, base, options) { |
| 32 | + return Promise.resolve(config.resolve) |
| 33 | + .then(function(resolve) { |
| 34 | + return resolve(id, path.join(root, base), options||{}); |
| 35 | + }) |
| 36 | + .then(function(result) { |
| 37 | + if (!(result instanceof Array)) result = [result]; |
| 38 | + return result.map(function(file) { |
| 39 | + return path.relative(root, file); |
| 40 | + }); |
| 41 | + }); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +fs.__setMockFiles([ |
| 46 | + 'app/Components/Box/blue.css', |
| 47 | + 'app/Components/Box/red.css', |
| 48 | + 'app/Theme/Components/Box/red.css', |
| 49 | +]); |
| 50 | + |
| 51 | +describe('postcss-import-sub', () => { |
| 52 | + it ('relative `to` substitution', () => { |
| 53 | + const render = mockRender(sub([ |
| 54 | + { |
| 55 | + id: new RegExp("red\.css"), |
| 56 | + to: "blue.css" |
| 57 | + } |
| 58 | + ])); |
| 59 | + return render("red.css", "app/Components/Box/") |
| 60 | + .then(function(module) { |
| 61 | + expect(module[0]).toBe("app/Components/Box/blue.css"); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it ('relative `to` substitution with dot', () => { |
| 66 | + const render = mockRender(sub([ |
| 67 | + { |
| 68 | + id: new RegExp("red\.css"), |
| 69 | + to: "./blue.css" |
| 70 | + } |
| 71 | + ])); |
| 72 | + return render("red.css", "app/Components/Box/") |
| 73 | + .then(function(module) { |
| 74 | + expect(module[0]).toBe("app/Components/Box/blue.css"); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it ('absolute `to` substitution', () => { |
| 79 | + const render = mockRender(sub([ |
| 80 | + { |
| 81 | + id: new RegExp("red\.css"), |
| 82 | + to: "<root>/app/Components/Box/blue.css" |
| 83 | + } |
| 84 | + ])); |
| 85 | + return render("red.css", "app/Components/Box/") |
| 86 | + .then(function(module) { |
| 87 | + expect(module[0]).toBe("app/Components/Box/blue.css"); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + it ('relative `path` substitution with dot', () => { |
| 92 | + const render = mockRender(sub([ |
| 93 | + { |
| 94 | + id: new RegExp("red\.css"), |
| 95 | + path: "<root>/app/Theme/Components/Box/" |
| 96 | + } |
| 97 | + ])); |
| 98 | + return render("./red.css", "app/Components/Box/") |
| 99 | + .then(function(module) { |
| 100 | + expect(module[0]).toBe("app/Theme/Components/Box/red.css"); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + it ('relative `path` substitution with without ending slash', () => { |
| 105 | + const render = mockRender(sub([ |
| 106 | + { |
| 107 | + id: new RegExp("red\.css"), |
| 108 | + path: "<root>/app/Theme/Components/Box" |
| 109 | + } |
| 110 | + ])); |
| 111 | + return render("red.css", "app/Components/Box/") |
| 112 | + .then(function(module) { |
| 113 | + expect(module[0]).toBe("app/Theme/Components/Box/red.css"); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + it ('absolute `to` substitution with alias', () => { |
| 118 | + const render = mockRender(sub([ |
| 119 | + { |
| 120 | + id: new RegExp("red\.css"), |
| 121 | + to: "<root>/app/Theme/Components/Box/<id>" |
| 122 | + } |
| 123 | + ])); |
| 124 | + return render("red.css", "app/Components/Box/") |
| 125 | + .then(function(module) { |
| 126 | + expect(module[0]).toBe("app/Theme/Components/Box/red.css"); |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + it ('absolute `to` substitution with alias', () => { |
| 131 | + const render = mockRender(sub([ |
| 132 | + { |
| 133 | + id: new RegExp("red\.css"), |
| 134 | + to: "<root>/app/Theme/Components/Box/<id>" |
| 135 | + } |
| 136 | + ])); |
| 137 | + return render("red.css", "app/Components/Box/") |
| 138 | + .then(function(module) { |
| 139 | + expect(module[0]).toBe("app/Theme/Components/Box/red.css"); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + it ('relative `path` with custom aliases', () => { |
| 144 | + const render = mockRender(sub([ |
| 145 | + { |
| 146 | + id: new RegExp("red\.css"), |
| 147 | + base: /Components\/([a-z0-9]+)\//i, |
| 148 | + path: "<root>/app/Theme/Components/<base:$1>/" |
| 149 | + } |
| 150 | + ])); |
| 151 | + return render("red.css", "app/Components/Box/") |
| 152 | + .then(function(module) { |
| 153 | + expect(module[0]).toBe("app/Theme/Components/Box/red.css"); |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + it ('absolute `to` with custom aliases', () => { |
| 158 | + const render = mockRender(sub([ |
| 159 | + { |
| 160 | + id: /([a-z0-9]+)\.css/, |
| 161 | + base: /Components\/([a-z0-9]+)\//i, |
| 162 | + to: "<root>/app/Theme/Components/<base:$1>/<id:$1>.css" |
| 163 | + } |
| 164 | + ])); |
| 165 | + return render("red.css", "app/Components/Box/") |
| 166 | + .then(function(module) { |
| 167 | + expect(module[0]).toBe("app/Theme/Components/Box/red.css"); |
| 168 | + }); |
| 169 | + }); |
| 170 | + |
| 171 | + it ('absolute `to` with custom aliases using module', () => { |
| 172 | + const render = mockRender(sub([ |
| 173 | + { |
| 174 | + module: /\/([a-z0-9]+)\/([a-z0-9]+)\.css/i, |
| 175 | + to: "<root>/app/Theme/Components/<module:$1>/<module:$2>.css" |
| 176 | + } |
| 177 | + ])); |
| 178 | + return render("red.css", "app/Components/Box/") |
| 179 | + .then(function(module) { |
| 180 | + expect(module[0]).toBe("app/Theme/Components/Box/red.css"); |
| 181 | + }); |
| 182 | + }); |
| 183 | +}); |
0 commit comments