|
| 1 | +import path from 'path'; |
| 2 | +const loader = require('../index').default; |
| 3 | +const { operator } = require('../index'); |
| 4 | + |
| 5 | +describe('js-to-sass-vars-loader', () => { |
| 6 | + |
| 7 | + describe('module', () => { |
| 8 | + const context = { |
| 9 | + context: path.resolve() |
| 10 | + }; |
| 11 | + |
| 12 | + it('exports a function', () => { |
| 13 | + expect(typeof loader).toEqual('function'); |
| 14 | + }); |
| 15 | + |
| 16 | + it('calls operator.mergeVarsToContent with content and loader context', () => { |
| 17 | + spyOn(operator, 'mergeVarsToContent'); |
| 18 | + loader.call(context, 'asdf'); |
| 19 | + expect(operator.mergeVarsToContent).toHaveBeenCalledWith('asdf', context); |
| 20 | + |
| 21 | + }); |
| 22 | + |
| 23 | + it('returns the result of mergeVarsToContent', () => { |
| 24 | + const content = 'require("./mocks/colors.js")\n' + '.someClass {\ncolor: $nice;\n}'; |
| 25 | + const merged = operator.mergeVarsToContent(content, context); |
| 26 | + const result = loader.call(context, content); |
| 27 | + expect(result).toEqual(merged); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('divideContent', () => { |
| 32 | + it('divides the require (if it exists) from the content', () => { |
| 33 | + const content = "require('colors.js');\n" + |
| 34 | + ".someClass { color: #fff;}"; |
| 35 | + expect(operator.divideContent(content)[0]).toEqual("require('colors.js');"); |
| 36 | + expect(operator.divideContent(content)[1]).toEqual("\n.someClass { color: #fff;}"); |
| 37 | + }); |
| 38 | + |
| 39 | + it('gives back content if there is no require in content', () => { |
| 40 | + const content = ".someClass { color: #fff;}"; |
| 41 | + expect(operator.divideContent(content)[0]).toEqual(""); |
| 42 | + expect(operator.divideContent(content)[1]).toEqual(content); |
| 43 | + }); |
| 44 | + |
| 45 | + it('handles more requires when divide', () => { |
| 46 | + const content = "require('colors.js');\n" + |
| 47 | + "require('sizes.js');\n" + |
| 48 | + ".someClass { color: #fff;}"; |
| 49 | + expect(operator.divideContent(content)[0]).toEqual("require('colors.js');\n" + "require('sizes.js');"); |
| 50 | + expect(operator.divideContent(content)[1]).toEqual("\n.someClass { color: #fff;}"); |
| 51 | + }); |
| 52 | + |
| 53 | + it('handles the form of request("asdf").someProp', () => { |
| 54 | + const content = "require('corners.js').typeOne;\n" + ".someClass { color: #fff;}"; |
| 55 | + expect(operator.divideContent(content)[0]).toEqual("require('corners.js').typeOne;"); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('getModulePath', () => { |
| 60 | + it('extracts module paths and methodName into an array', () => { |
| 61 | + expect(operator.getModulePath('require("./mocks/colors.js");\n')).toEqual([{path: "./mocks/colors.js"}]); |
| 62 | + |
| 63 | + expect(operator.getModulePath('require("./mocks/colors.js");\n' + 'require("./mocks/sizes.js");')).toEqual([{path: "./mocks/colors.js"}, {path:"./mocks/sizes.js"}]); |
| 64 | + |
| 65 | + expect(operator.getModulePath('require("./mocks/corners.js").typeTwo;\n')).toEqual([{path: "./mocks/corners.js", methodName: 'typeTwo'}]); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + describe('getVarData', () => { |
| 70 | + const context = { context: path.resolve()}; |
| 71 | + |
| 72 | + it('gets variable data by modulePath with context', () => { |
| 73 | + const varData = operator.getVarData([{path: './mocks/colors.js' }], context); |
| 74 | + expect(varData).toEqual({ white: '#fff', black: '#000'}); |
| 75 | + }); |
| 76 | + |
| 77 | + it('merges module data if there are more requests', () => { |
| 78 | + const varData = operator.getVarData([{path:'./mocks/colors.js'}, {path:'./mocks/sizes.js'}], context); |
| 79 | + expect(varData).toEqual({ white: '#fff', black: '#000', small: '10px', large: '50px'}); |
| 80 | + }); |
| 81 | + |
| 82 | + it('handles methodName if it is given', () => { |
| 83 | + const varData = operator.getVarData([{ path:'./mocks/corners.js', methodName: 'typeOne'}], context); |
| 84 | + expect(varData).toEqual({ tiny: '1%', medium: '3%'}); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('transformToSassVars', () => { |
| 89 | + it('takes a hash object and transforms it to sass variables', () => { |
| 90 | + const colors = require('../mocks/colors.js'); |
| 91 | + expect(operator.transformToSassVars(colors)).toEqual('$white: #fff;\n$black: #000;\n'); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + describe('mergeVarsToContent', () => { |
| 96 | + const context = { |
| 97 | + context: path.resolve() |
| 98 | + }; |
| 99 | + |
| 100 | + it('inserst vars to sass content', () => { |
| 101 | + const content = "require('./mocks/colors.js');\n" + |
| 102 | + ".someClass { color: #fff;}"; |
| 103 | + const [ moduleData, sassContent ] = operator.divideContent(content); |
| 104 | + const modulePath = operator.getModulePath(moduleData); |
| 105 | + const varData = operator.getVarData(modulePath, context); |
| 106 | + const sassVars = operator.transformToSassVars(varData); |
| 107 | + |
| 108 | + expect(operator.mergeVarsToContent(content, context)).toEqual(sassVars + sassContent); |
| 109 | + }); |
| 110 | + |
| 111 | + it('gives back content as is if there is no requre', () => { |
| 112 | + const content = ".someClass { color: #fff;}"; |
| 113 | + expect(operator.mergeVarsToContent(content, context)).toEqual(content); |
| 114 | + }); |
| 115 | + }); |
| 116 | +}); |
0 commit comments