|
| 1 | +describe('layout directives', function() { |
| 2 | + |
| 3 | + beforeEach(module('material.core')); |
| 4 | + |
| 5 | + describe('expecting layout classes', function() { |
| 6 | + |
| 7 | + var suffixes = ['sm', 'gt-sm', 'md', 'gt-md', 'lg', 'gt-lg']; |
| 8 | + |
| 9 | + var directionValues = ['row', 'column']; |
| 10 | + var flexOrderValues = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
| 11 | + var flexValues = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 33, 34, 66, 67]; |
| 12 | + var offsetValues = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 33, 34, 66, 67]; |
| 13 | + var alignmentValues = [ |
| 14 | + 'center', 'center center', 'center start', 'center end', |
| 15 | + 'end', 'end-center', 'end start', 'end end', |
| 16 | + 'space-around', 'space-around center', 'space-around start', 'space-around end', |
| 17 | + 'space-between', 'space-between center', 'space-between start', 'space-between end', |
| 18 | + 'center center', 'start center', 'end center', 'space-between center', 'space-around center', |
| 19 | + 'center start', 'start start', 'end start', 'space-between start', 'space-around start', |
| 20 | + 'center end', 'start end', 'end end', 'space-between end', 'space-around end' |
| 21 | + ]; |
| 22 | + |
| 23 | + var mappings = [ |
| 24 | + { attribute: 'layout', suffixes: suffixes, values: directionValues, addDirectiveAsClass: true, testStandAlone: true }, |
| 25 | + { attribute: 'layout-align', suffixes: suffixes, values: alignmentValues }, |
| 26 | + { attribute: 'layout-padding', testStandAlone: true }, |
| 27 | + { attribute: 'layout-margin', testStandAlone: true }, |
| 28 | + { attribute: 'layout-wrap', testStandAlone: true }, |
| 29 | + { attribute: 'layout-fill', testStandAlone: true }, |
| 30 | + { attribute: 'flex', suffixes: suffixes, values: flexValues, addDirectiveAsClass: true, testStandAlone: true}, |
| 31 | + { attribute: 'flex-order', suffixes: suffixes, values: flexOrderValues }, |
| 32 | + { attribute: 'offset', suffixes: suffixes, values: offsetValues }, |
| 33 | + { attribute: 'hide', suffixes: suffixes, testStandAlone: true }, |
| 34 | + { attribute: 'show', suffixes: suffixes, testStandAlone: true } |
| 35 | + ]; |
| 36 | + |
| 37 | + // Run all the tests; iterating the mappings... |
| 38 | + |
| 39 | + for (var i = 0; i < mappings.length; i++) { |
| 40 | + var map = mappings[i]; |
| 41 | + |
| 42 | + if (map.testStandAlone) testMapping(map.attribute, map.attribute); |
| 43 | + if (map.suffixes) testWithSuffix(map.attribute, map.suffixes, map.values, map.testStandAlone, map.addDirectiveAsClass); |
| 44 | + if (map.values) testWithSuffixAndValue(map.attribute, map.values, undefined, map.addDirectiveAsClass); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * |
| 49 | + */ |
| 50 | + function testMapping(attribute, expectedClass) { |
| 51 | + it('should fail if the class ' + expectedClass + ' was not added for attribute ' + attribute, inject(function($compile, $rootScope) { |
| 52 | + var element = $compile('<div ' + attribute + '>Layout</div>')($rootScope); |
| 53 | + expect(element.hasClass(expectedClass)).toBe(true); |
| 54 | + })); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * |
| 59 | + */ |
| 60 | + function testWithSuffixAndValue(attribute, values, suffix, addDirectiveAsClass) { |
| 61 | + |
| 62 | + for (var j = 0; j < values.length; j++) { |
| 63 | + var value = values[j].toString(); |
| 64 | + var attr = suffix ? attribute + '-' + suffix : attribute; |
| 65 | + |
| 66 | + var attrWithValue = buildAttributeWithValue(attr, value); |
| 67 | + var expectedClass = buildExpectedClass(attr, value); |
| 68 | + |
| 69 | + // Run each test. |
| 70 | + testMapping(attrWithValue, expectedClass); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Build string of expected classes that should be added to the |
| 75 | + * DOM element. |
| 76 | + * |
| 77 | + * Convert directive with value to classes |
| 78 | + * |
| 79 | + * @param attrClass String full attribute name; eg 'layout-gt-lg' |
| 80 | + * @param attrValue String HTML directive; eg "column" |
| 81 | + * |
| 82 | + * @returns String to be used with element.addClass(...); eg `layout-gt-lg layout-gt-lg-column` |
| 83 | + */ |
| 84 | + function buildExpectedClass(attrClass, attrValue) { |
| 85 | + if (addDirectiveAsClass) attrClass += ' ' + attrClass; |
| 86 | + return attrClass + "-" + attrValue.replace(/\s+/g, "-"); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Build full string of expected directive with its value |
| 91 | + * Note: The expected class always starts with the |
| 92 | + * attribute name, add the suffix if any. |
| 93 | + * |
| 94 | + * @param attrClass String full attribute name; eg 'layout-gt-lg' |
| 95 | + * @param attrValue String HTML directive; eg "column" |
| 96 | + * |
| 97 | + * @returns String like `layout-gt-lg="column"` |
| 98 | + */ |
| 99 | + function buildAttributeWithValue(attr, value) { |
| 100 | + return attr + '="' + value + '"'; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * |
| 106 | + */ |
| 107 | + function testWithSuffix(attribute, suffixes, values, testStandAlone, addDirectiveAsClass) { |
| 108 | + for (var j = 0; j < suffixes.length; j++) { |
| 109 | + var suffix = suffixes[j]; |
| 110 | + var attr = attribute + '-' + suffix; |
| 111 | + |
| 112 | + if (testStandAlone) testMapping(attr, attr); |
| 113 | + if (values) testWithSuffixAndValue(attribute, values, suffix, addDirectiveAsClass); |
| 114 | + } |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | +}); |
0 commit comments