|
| 1 | +import path from 'path'; |
| 2 | + |
| 3 | +import express from 'express'; |
| 4 | + |
| 5 | +import middleware from '../../src'; |
| 6 | +import getFilenameFromUrl from '../../src/utils/getFilenameFromUrl'; |
| 7 | + |
| 8 | +import getCompiler from '../helpers/getCompiler'; |
| 9 | +import listenAndCompile from '../helpers/listenAndCompile'; |
| 10 | +import webpackSimpleConfig from '../fixtures/webpack.simple.config'; |
| 11 | +import webpackPublicPathConfig from '../fixtures/webpack.public-path.config'; |
| 12 | +import webpackMultiConfig from '../fixtures/webpack.array.config'; |
| 13 | + |
| 14 | +describe('getFilenameFromUrl', () => { |
| 15 | + const configs = [ |
| 16 | + { |
| 17 | + title: 'simple config with path /', |
| 18 | + config: webpackSimpleConfig, |
| 19 | + middlewareConfig: {}, |
| 20 | + url: '/', |
| 21 | + expected: path.resolve(__dirname, '../outputs/simple/index.html'), |
| 22 | + }, |
| 23 | + { |
| 24 | + title: 'simple config with path /index.html', |
| 25 | + config: webpackSimpleConfig, |
| 26 | + middlewareConfig: {}, |
| 27 | + url: '/index.html', |
| 28 | + expected: path.resolve(__dirname, '../outputs/simple/index.html'), |
| 29 | + }, |
| 30 | + { |
| 31 | + title: 'simple config with path /path', |
| 32 | + config: webpackSimpleConfig, |
| 33 | + middlewareConfig: {}, |
| 34 | + url: '/path', |
| 35 | + expected: path.resolve(__dirname, '../outputs/simple/path'), |
| 36 | + }, |
| 37 | + { |
| 38 | + title: 'simple config with path /path/file.html', |
| 39 | + config: webpackSimpleConfig, |
| 40 | + middlewareConfig: {}, |
| 41 | + url: '/path/file.html', |
| 42 | + expected: path.resolve(__dirname, '../outputs/simple/path/file.html'), |
| 43 | + }, |
| 44 | + { |
| 45 | + title: 'simple config with index false and path /', |
| 46 | + config: webpackSimpleConfig, |
| 47 | + middlewareConfig: { |
| 48 | + index: false, |
| 49 | + }, |
| 50 | + url: '/', |
| 51 | + expected: path.resolve(__dirname, '../outputs/simple'), |
| 52 | + }, |
| 53 | + { |
| 54 | + title: 'simple config with index file.html and path /', |
| 55 | + config: webpackSimpleConfig, |
| 56 | + middlewareConfig: { |
| 57 | + index: 'file.html', |
| 58 | + }, |
| 59 | + url: '/', |
| 60 | + expected: path.resolve(__dirname, '../outputs/simple/file.html'), |
| 61 | + }, |
| 62 | + |
| 63 | + { |
| 64 | + title: 'publicPath config with path /', |
| 65 | + config: webpackPublicPathConfig, |
| 66 | + middlewareConfig: {}, |
| 67 | + url: '/', |
| 68 | + expected: null, |
| 69 | + }, |
| 70 | + { |
| 71 | + title: 'publicPath config with path /public/path/', |
| 72 | + config: webpackPublicPathConfig, |
| 73 | + middlewareConfig: {}, |
| 74 | + url: '/public/path/', |
| 75 | + expected: path.resolve(__dirname, '../outputs/public-path/index.html'), |
| 76 | + }, |
| 77 | + { |
| 78 | + title: 'publicPath config with path /public/path/more/file.html', |
| 79 | + config: webpackPublicPathConfig, |
| 80 | + middlewareConfig: {}, |
| 81 | + url: '/public/path/more/file.html', |
| 82 | + expected: path.resolve( |
| 83 | + __dirname, |
| 84 | + '../outputs/public-path/more/file.html' |
| 85 | + ), |
| 86 | + }, |
| 87 | + |
| 88 | + { |
| 89 | + title: 'multi config with path /', |
| 90 | + config: webpackMultiConfig, |
| 91 | + middlewareConfig: {}, |
| 92 | + url: '/', |
| 93 | + expected: null, |
| 94 | + }, |
| 95 | + { |
| 96 | + title: 'multi config with path /static-one/', |
| 97 | + config: webpackMultiConfig, |
| 98 | + middlewareConfig: {}, |
| 99 | + url: '/static-one/', |
| 100 | + expected: path.resolve(__dirname, '../outputs/array/js1/index.html'), |
| 101 | + }, |
| 102 | + { |
| 103 | + title: 'multi config with path /static-two/', |
| 104 | + config: webpackMultiConfig, |
| 105 | + middlewareConfig: {}, |
| 106 | + url: '/static-two/', |
| 107 | + expected: path.resolve(__dirname, '../outputs/array/js2/index.html'), |
| 108 | + }, |
| 109 | + ]; |
| 110 | + |
| 111 | + configs.forEach((config) => { |
| 112 | + describe(config.title, () => { |
| 113 | + let instance; |
| 114 | + let listen; |
| 115 | + let app; |
| 116 | + let compiler; |
| 117 | + |
| 118 | + beforeEach((done) => { |
| 119 | + compiler = getCompiler(config.config); |
| 120 | + |
| 121 | + instance = middleware(compiler, config.middlewareConfig); |
| 122 | + |
| 123 | + app = express(); |
| 124 | + app.use(instance); |
| 125 | + |
| 126 | + listen = listenAndCompile(app, compiler, done); |
| 127 | + }); |
| 128 | + |
| 129 | + afterEach((done) => { |
| 130 | + if (instance) { |
| 131 | + instance.close(); |
| 132 | + } |
| 133 | + |
| 134 | + if (listen) { |
| 135 | + listen.close(done); |
| 136 | + } else { |
| 137 | + done(); |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + it('should return correct filename from url', () => { |
| 142 | + const filename = getFilenameFromUrl(instance.context, config.url); |
| 143 | + const { expected } = config; |
| 144 | + if (expected) { |
| 145 | + expect(filename).toEqual(expected); |
| 146 | + } else { |
| 147 | + expect(filename).toBeUndefined(); |
| 148 | + } |
| 149 | + }); |
| 150 | + }); |
| 151 | + }); |
| 152 | +}); |
0 commit comments