|
| 1 | +var Checker = require('../../../lib/checker'); |
| 2 | +var expect = require('chai').expect; |
| 3 | + |
| 4 | +describe('rules/require-imports-aplhabetized', function() { |
| 5 | + var checker; |
| 6 | + |
| 7 | + beforeEach(function() { |
| 8 | + checker = new Checker(); |
| 9 | + checker.registerDefaultRules(); |
| 10 | + checker.configure({ requireImportAlphabetized: true }); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should report no errors when there are no imports', function() { |
| 14 | + var code = '// no imports here;\n' + |
| 15 | + 'var looping = function(n) {\n' + |
| 16 | + 'var a = 0, b = 1, f = 1;\n' + |
| 17 | + 'for(var i = 2; i <= n; i++) {\n' + |
| 18 | + 'f = a + b;\n' + |
| 19 | + 'a = b;\n' + |
| 20 | + 'b = f;\n' + |
| 21 | + '}\n' + |
| 22 | + 'return f; \n' + |
| 23 | + '};'; |
| 24 | + |
| 25 | + expect(checker.checkString(code)).to.have.no.errors(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should report no errors for ordered imports', function() { |
| 29 | + var code = 'import A from \'A\';\nimport C from \'C\';\nimport Z from \'Z\';'; |
| 30 | + expect(checker.checkString(code)).to.have.no.errors(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should report no errors for ordered import multiline', function() { |
| 34 | + var code = 'import A\n from\n \'A\';\nimport C\n from \'C\';\nimport Z\n from \'Z\';'; |
| 35 | + expect(checker.checkString(code)).to.have.no.errors(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should report one error for un-ordered imports', function() { |
| 39 | + var code = 'import A from \'A\';\nimport Z from \'Z\';\nimport B from \'B\';'; |
| 40 | + expect(checker.checkString(code)).to.have.one.error(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should report no errors for ordered named imports', function() { |
| 44 | + var code = 'import { A as myNamed1, named2 } from \'src/mylib\';\n' + |
| 45 | + 'import { B as myNamed1, named2 } from \'src/mylib\';\n' + |
| 46 | + 'import { C as myNamed1, named2 } from \'src/mylib\';'; |
| 47 | + |
| 48 | + expect(checker.checkString(code)).to.have.no.errors(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should report one error for un-ordered named imports', function() { |
| 52 | + var code = 'import { Z as myNamed1, named2 } from \'src/mylib\';\n' + |
| 53 | + 'import { B as myNamed1, named2 } from \'src/mylib\';\n' + |
| 54 | + 'import { C as myNamed1, named2 } from \'src/mylib\';'; |
| 55 | + |
| 56 | + expect(checker.checkString(code)).to.have.one.error(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should report no errors for ordered module as an object import', function() { |
| 60 | + var code = 'import * from \'src/A\';\n' + |
| 61 | + 'import * from \'src/B\';\n' + |
| 62 | + 'import * from \'src/C\';'; |
| 63 | + |
| 64 | + expect(checker.checkString(code)).to.have.no.error(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should report one error for un-ordered module as an object import', function() { |
| 68 | + var code = 'import * from \'src/A\';\n' + |
| 69 | + 'import * from \'src/Z\';\n' + |
| 70 | + 'import * from \'src/C\';'; |
| 71 | + |
| 72 | + expect(checker.checkString(code)).to.have.one.error(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should report no errors for ordered module import', function() { |
| 76 | + var code = 'import \'src/A\';\n' + |
| 77 | + 'import \'src/B\';\n' + |
| 78 | + 'import \'src/C\';'; |
| 79 | + |
| 80 | + expect(checker.checkString(code)).to.have.no.error(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should report one error for un-ordered module import', function() { |
| 84 | + var code = 'import \'src/Z\';\n import \'src/B\';\nimport \'src/C\';'; |
| 85 | + expect(checker.checkString(code)).to.have.one.error(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should report multiple errors for un-ordered module import', function() { |
| 89 | + var code = 'import \'src/C\';\n import \'src/B\';\nimport \'src/A\';'; |
| 90 | + expect(checker.checkString(code)).to.have.validation.errors.from('requireImportAlphabetized'); |
| 91 | + }); |
| 92 | +}); |
0 commit comments