|
| 1 | +import type * as d from '@stencil/core/declarations'; |
| 2 | +import { mockConfig } from '@stencil/core/testing'; |
| 3 | +import { COPY, DIST_CUSTOM_ELEMENTS, DIST_TYPES } from '../../output-targets/output-utils'; |
| 4 | +import { validateConfig } from '../validate-config'; |
| 5 | +import path from 'path'; |
| 6 | + |
| 7 | +describe('validate-output-dist-custom-element', () => { |
| 8 | + describe('validateCustomElement', () => { |
| 9 | + const rootDir = path.resolve('/'); |
| 10 | + const defaultDistDir = path.join(rootDir, 'dist', 'components'); |
| 11 | + const distCustomElementsDir = 'my-dist-custom-elements'; |
| 12 | + let userConfig: d.Config; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + userConfig = mockConfig(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('generates a default dist-custom-elements output target', () => { |
| 19 | + const outputTarget: d.OutputTargetDistCustomElements = { |
| 20 | + type: DIST_CUSTOM_ELEMENTS, |
| 21 | + }; |
| 22 | + userConfig.outputTargets = [outputTarget]; |
| 23 | + |
| 24 | + const { config } = validateConfig(userConfig); |
| 25 | + expect(config.outputTargets).toEqual([ |
| 26 | + { |
| 27 | + type: DIST_CUSTOM_ELEMENTS, |
| 28 | + copy: [], |
| 29 | + dir: defaultDistDir, |
| 30 | + empty: true, |
| 31 | + externalRuntime: true, |
| 32 | + }, |
| 33 | + ]); |
| 34 | + }); |
| 35 | + |
| 36 | + it('uses a provided dir field over a default directory', () => { |
| 37 | + const outputTarget: d.OutputTargetDistCustomElements = { |
| 38 | + type: DIST_CUSTOM_ELEMENTS, |
| 39 | + dir: distCustomElementsDir, |
| 40 | + }; |
| 41 | + userConfig.outputTargets = [outputTarget]; |
| 42 | + |
| 43 | + const { config } = validateConfig(userConfig); |
| 44 | + expect(config.outputTargets).toEqual([ |
| 45 | + { |
| 46 | + type: DIST_CUSTOM_ELEMENTS, |
| 47 | + copy: [], |
| 48 | + dir: path.join(rootDir, distCustomElementsDir), |
| 49 | + empty: true, |
| 50 | + externalRuntime: true, |
| 51 | + }, |
| 52 | + ]); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('"empty" field', () => { |
| 56 | + it('defaults the "empty" field to true if not provided', () => { |
| 57 | + const outputTarget: d.OutputTargetDistCustomElements = { |
| 58 | + type: DIST_CUSTOM_ELEMENTS, |
| 59 | + externalRuntime: false, |
| 60 | + }; |
| 61 | + userConfig.outputTargets = [outputTarget]; |
| 62 | + |
| 63 | + const { config } = validateConfig(userConfig); |
| 64 | + expect(config.outputTargets).toEqual([ |
| 65 | + { |
| 66 | + type: DIST_CUSTOM_ELEMENTS, |
| 67 | + copy: [], |
| 68 | + dir: defaultDistDir, |
| 69 | + empty: true, |
| 70 | + externalRuntime: false, |
| 71 | + }, |
| 72 | + ]); |
| 73 | + }); |
| 74 | + |
| 75 | + it('defaults the "empty" field to true it\'s not a boolean', () => { |
| 76 | + const outputTarget: d.OutputTargetDistCustomElements = { |
| 77 | + type: DIST_CUSTOM_ELEMENTS, |
| 78 | + empty: undefined, |
| 79 | + externalRuntime: false, |
| 80 | + }; |
| 81 | + userConfig.outputTargets = [outputTarget]; |
| 82 | + |
| 83 | + const { config } = validateConfig(userConfig); |
| 84 | + expect(config.outputTargets).toEqual([ |
| 85 | + { |
| 86 | + type: DIST_CUSTOM_ELEMENTS, |
| 87 | + copy: [], |
| 88 | + dir: defaultDistDir, |
| 89 | + empty: true, |
| 90 | + externalRuntime: false, |
| 91 | + }, |
| 92 | + ]); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe('"externalRuntime" field', () => { |
| 97 | + it('defaults the "externalRuntime" field to true if not provided', () => { |
| 98 | + const outputTarget: d.OutputTargetDistCustomElements = { |
| 99 | + type: DIST_CUSTOM_ELEMENTS, |
| 100 | + empty: false, |
| 101 | + }; |
| 102 | + userConfig.outputTargets = [outputTarget]; |
| 103 | + |
| 104 | + const { config } = validateConfig(userConfig); |
| 105 | + expect(config.outputTargets).toEqual([ |
| 106 | + { |
| 107 | + type: DIST_CUSTOM_ELEMENTS, |
| 108 | + copy: [], |
| 109 | + dir: defaultDistDir, |
| 110 | + empty: false, |
| 111 | + externalRuntime: true, |
| 112 | + }, |
| 113 | + ]); |
| 114 | + }); |
| 115 | + |
| 116 | + it('defaults the "externalRuntime" field to true it\'s not a boolean', () => { |
| 117 | + const outputTarget: d.OutputTargetDistCustomElements = { |
| 118 | + type: DIST_CUSTOM_ELEMENTS, |
| 119 | + empty: false, |
| 120 | + externalRuntime: undefined, |
| 121 | + }; |
| 122 | + userConfig.outputTargets = [outputTarget]; |
| 123 | + |
| 124 | + const { config } = validateConfig(userConfig); |
| 125 | + expect(config.outputTargets).toEqual([ |
| 126 | + { |
| 127 | + type: DIST_CUSTOM_ELEMENTS, |
| 128 | + copy: [], |
| 129 | + dir: defaultDistDir, |
| 130 | + empty: false, |
| 131 | + externalRuntime: true, |
| 132 | + }, |
| 133 | + ]); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('"generateTypeDeclarations" field', () => { |
| 138 | + it('creates a types directory when "generateTypeDeclarations" is true', () => { |
| 139 | + const outputTarget: d.OutputTargetDistCustomElements = { |
| 140 | + type: DIST_CUSTOM_ELEMENTS, |
| 141 | + empty: false, |
| 142 | + externalRuntime: false, |
| 143 | + generateTypeDeclarations: true, |
| 144 | + }; |
| 145 | + userConfig.outputTargets = [outputTarget]; |
| 146 | + |
| 147 | + const { config } = validateConfig(userConfig); |
| 148 | + expect(config.outputTargets).toEqual([ |
| 149 | + { |
| 150 | + type: DIST_TYPES, |
| 151 | + dir: defaultDistDir, |
| 152 | + typesDir: path.join(rootDir, 'dist', 'types'), |
| 153 | + }, |
| 154 | + { |
| 155 | + type: DIST_CUSTOM_ELEMENTS, |
| 156 | + copy: [], |
| 157 | + dir: defaultDistDir, |
| 158 | + empty: false, |
| 159 | + externalRuntime: false, |
| 160 | + generateTypeDeclarations: true, |
| 161 | + }, |
| 162 | + ]); |
| 163 | + }); |
| 164 | + |
| 165 | + it('creates a types directory for a custom directory', () => { |
| 166 | + const outputTarget: d.OutputTargetDistCustomElements = { |
| 167 | + type: DIST_CUSTOM_ELEMENTS, |
| 168 | + dir: distCustomElementsDir, |
| 169 | + empty: false, |
| 170 | + externalRuntime: false, |
| 171 | + generateTypeDeclarations: true, |
| 172 | + }; |
| 173 | + userConfig.outputTargets = [outputTarget]; |
| 174 | + |
| 175 | + const { config } = validateConfig(userConfig); |
| 176 | + expect(config.outputTargets).toEqual([ |
| 177 | + { |
| 178 | + type: DIST_TYPES, |
| 179 | + dir: path.join(rootDir, distCustomElementsDir), |
| 180 | + typesDir: path.join(rootDir, 'dist', 'types'), |
| 181 | + }, |
| 182 | + { |
| 183 | + type: DIST_CUSTOM_ELEMENTS, |
| 184 | + copy: [], |
| 185 | + dir: path.join(rootDir, distCustomElementsDir), |
| 186 | + empty: false, |
| 187 | + externalRuntime: false, |
| 188 | + generateTypeDeclarations: true, |
| 189 | + }, |
| 190 | + ]); |
| 191 | + }); |
| 192 | + |
| 193 | + it('doesn\'t create a types directory when "generateTypeDeclarations" is false', () => { |
| 194 | + const outputTarget: d.OutputTargetDistCustomElements = { |
| 195 | + type: DIST_CUSTOM_ELEMENTS, |
| 196 | + empty: false, |
| 197 | + externalRuntime: false, |
| 198 | + generateTypeDeclarations: false, |
| 199 | + }; |
| 200 | + userConfig.outputTargets = [outputTarget]; |
| 201 | + |
| 202 | + const { config } = validateConfig(userConfig); |
| 203 | + expect(config.outputTargets).toEqual([ |
| 204 | + { |
| 205 | + type: DIST_CUSTOM_ELEMENTS, |
| 206 | + copy: [], |
| 207 | + dir: defaultDistDir, |
| 208 | + empty: false, |
| 209 | + externalRuntime: false, |
| 210 | + generateTypeDeclarations: false, |
| 211 | + }, |
| 212 | + ]); |
| 213 | + }); |
| 214 | + }); |
| 215 | + |
| 216 | + describe('copy tasks', () => { |
| 217 | + it('copies existing copy tasks over to the output target', () => { |
| 218 | + const copyOutputTarget: d.CopyTask = { |
| 219 | + src: 'mock/src', |
| 220 | + dest: 'mock/dest', |
| 221 | + }; |
| 222 | + const copyOutputTarget2: d.CopyTask = { |
| 223 | + src: 'mock/src2', |
| 224 | + dest: 'mock/dest2', |
| 225 | + }; |
| 226 | + |
| 227 | + const outputTarget: d.OutputTargetDistCustomElements = { |
| 228 | + type: DIST_CUSTOM_ELEMENTS, |
| 229 | + copy: [copyOutputTarget, copyOutputTarget2], |
| 230 | + dir: distCustomElementsDir, |
| 231 | + empty: false, |
| 232 | + externalRuntime: false, |
| 233 | + }; |
| 234 | + userConfig.outputTargets = [outputTarget]; |
| 235 | + |
| 236 | + const { config } = validateConfig(userConfig); |
| 237 | + expect(config.outputTargets).toEqual([ |
| 238 | + { |
| 239 | + type: COPY, |
| 240 | + dir: rootDir, |
| 241 | + copy: [copyOutputTarget, copyOutputTarget2], |
| 242 | + }, |
| 243 | + { |
| 244 | + type: DIST_CUSTOM_ELEMENTS, |
| 245 | + copy: [copyOutputTarget, copyOutputTarget2], |
| 246 | + dir: path.join(rootDir, distCustomElementsDir), |
| 247 | + empty: false, |
| 248 | + externalRuntime: false, |
| 249 | + }, |
| 250 | + ]); |
| 251 | + }); |
| 252 | + }); |
| 253 | + }); |
| 254 | +}); |
0 commit comments