|
| 1 | +import tmp from 'tmp'; |
| 2 | +import path from 'path'; |
| 3 | +import fs from 'fs-extra'; |
| 4 | +import { minify } from 'html-minifier'; |
| 5 | + |
| 6 | +import rcs from '../lib'; |
| 7 | +import reset from './helpers/reset'; |
| 8 | + |
| 9 | +let testCwd; |
| 10 | +const testFiles = '__tests__/files'; |
| 11 | +const fixturesCwd = path.join(testFiles, 'fixtures'); |
| 12 | + |
| 13 | +beforeEach(() => { |
| 14 | + testCwd = tmp.dirSync(); |
| 15 | + |
| 16 | + reset(); |
| 17 | +}); |
| 18 | + |
| 19 | +afterEach(() => { |
| 20 | + testCwd.removeCallback(); |
| 21 | +}); |
| 22 | + |
| 23 | +test('issue #19 | detect one file as array', async () => { |
| 24 | + await rcs.process.css('**/style.css', { |
| 25 | + newPath: testCwd.name, |
| 26 | + cwd: fixturesCwd, |
| 27 | + }); |
| 28 | + |
| 29 | + await rcs.process.html(['html/index.html'], { |
| 30 | + newPath: testCwd.name, |
| 31 | + cwd: fixturesCwd, |
| 32 | + }); |
| 33 | + |
| 34 | + expect(fs.existsSync(path.join(testCwd.name, '/html/index.html'))).toBe(true); |
| 35 | + expect(fs.existsSync(path.join(testCwd.name, '/css/style.css'))).toBe(true); |
| 36 | + expect(fs.existsSync(path.join(testCwd.name, '/css/subdirectory/style.css'))).toBe(true); |
| 37 | +}); |
| 38 | + |
| 39 | +test('issue #19 | detect one file', async () => { |
| 40 | + await rcs.process.css('**/style.css', { |
| 41 | + newPath: testCwd.name, |
| 42 | + cwd: fixturesCwd, |
| 43 | + }); |
| 44 | + |
| 45 | + await rcs.process.html('html/index.html', { |
| 46 | + newPath: testCwd.name, |
| 47 | + cwd: fixturesCwd, |
| 48 | + }); |
| 49 | + |
| 50 | + expect(fs.existsSync(path.join(testCwd.name, '/html/index.html'))).toBe(true); |
| 51 | + expect(fs.existsSync(path.join(testCwd.name, '/css/style.css'))).toBe(true); |
| 52 | + expect(fs.existsSync(path.join(testCwd.name, '/css/subdirectory/style.css'))).toBe(true); |
| 53 | +}); |
| 54 | + |
| 55 | +test('issue #21 | with auto', async () => { |
| 56 | + const issueFixture = path.join(testFiles, 'issue21/fixtures'); |
| 57 | + const issueResults = path.join(testFiles, 'issue21/results'); |
| 58 | + |
| 59 | + await rcs.process.auto(['style.css', 'index.html'], { |
| 60 | + newPath: testCwd.name, |
| 61 | + cwd: issueFixture, |
| 62 | + }); |
| 63 | + |
| 64 | + const newCss = fs.readFileSync(path.join(testCwd.name, '/style.css'), 'utf8'); |
| 65 | + const newHtml = fs.readFileSync(path.join(testCwd.name, '/index.html'), 'utf8'); |
| 66 | + const expectedCss = fs.readFileSync(path.join(issueResults, '/style.css'), 'utf8'); |
| 67 | + const expectedHtml = fs.readFileSync(path.join(issueResults, '/index.html'), 'utf8'); |
| 68 | + |
| 69 | + expect(newCss).toBe(expectedCss); |
| 70 | + expect(minify(newHtml, { collapseWhitespace: true })) |
| 71 | + .toBe(minify(expectedHtml, { collapseWhitespace: true })); |
| 72 | +}); |
| 73 | + |
| 74 | +test('issue #21 | with seperated process functions', async () => { |
| 75 | + const issueFixture = path.join(testFiles, 'issue21/fixtures'); |
| 76 | + const issueResults = path.join(testFiles, 'issue21/results'); |
| 77 | + |
| 78 | + await rcs.process.css('style.css', { |
| 79 | + newPath: testCwd.name, |
| 80 | + cwd: issueFixture, |
| 81 | + }); |
| 82 | + |
| 83 | + await rcs.process.html('index.html', { |
| 84 | + newPath: testCwd.name, |
| 85 | + cwd: issueFixture, |
| 86 | + }); |
| 87 | + |
| 88 | + const newCss = fs.readFileSync(path.join(testCwd.name, '/style.css'), 'utf8'); |
| 89 | + const newHtml = fs.readFileSync(path.join(testCwd.name, '/index.html'), 'utf8'); |
| 90 | + const expectedCss = fs.readFileSync(path.join(issueResults, '/style.css'), 'utf8'); |
| 91 | + const expectedHtml = fs.readFileSync(path.join(issueResults, '/index.html'), 'utf8'); |
| 92 | + |
| 93 | + expect(newCss).toBe(expectedCss); |
| 94 | + expect(minify(newHtml, { collapseWhitespace: true })) |
| 95 | + .toBe(minify(expectedHtml, { collapseWhitespace: true })); |
| 96 | +}); |
0 commit comments