aboutsummaryrefslogtreecommitdiff
path: root/tests/unit-tests/styles-controller-test.js
blob: b98c2f51f00961672c06df59a89220d5dfb083f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
 * Created by JetBrains WebStorm.
 * User: kfg834
 * Date: 12/27/11
 * Time: 10:52 AM
 * To change this template use File | Settings | File Templates.
 */
var Montage = require("montage/core/core").Montage;
var cmObject = require("ninjaapp/js/controllers/styles-controller"),
    cm = cmObject.StylesController;


describe('StylesController', function() {

    
    function makeAndAppend(tag, attr) {
        var el = document.createElement(tag);
        if (typeof attr === 'object') {
            for (var a in attr) {
                if (attr.hasOwnProperty(a)) {
                    el[a] = attr[a];
                }
            }
        } else if (typeof attr === 'string') {
            el.className = (el.className + ' ' + attr).trim();
        }

        document.body.appendChild(el);

        return el;
    }

    /** **************** Rule tests **************** */
    
    
    /* Add a Rule */
    describe('addRule', function() {
        it('Add Rule', function() {
            cm.activeDocument = { "_document": document };
            var rule = cm.addRule('div#Div_1 { background-color: rgb(0, 0, 0) }');
            expect(rule.cssText).toEqual("div#Div_1 { background-color: rgb(0, 0, 0); }");

        });

    });
    
    /* Create OverRide Rule */
    describe('createOverrideRule', function() {
        it('Override rule created successfully', function() {
            var object = sm.createOverrideRule('div#Div_1 { background-color: black }', 'div');
            expect(object.rule).toEqual("div#Div_1 { background-color: black }");
            
        });

    });
    
    
    /*Delete a Rule*/
    describe('Delete Rule', function() {
        it('Rule deleted successfully', function() {
             var index = sm.deleteRule('div#Div_1 { background-color: black }');
             expect(index).toBeGreaterThan(-1);
        });

    });


    /* Get Dominant Rule for an Element */
    describe('GetDominantRule-Element', function() {

        it('Got Dominant rule for element successfully', function() {

                var rules = ['div#Div_1 { background-color: black }',
                    '#UserContent div#Div_1 { background-color: blue }',
                    '#UserContent #Div_1 { background-color: white }',
                    'div div#Div_1 { background-color: red }'];

                     rules.forEach(function(rule) {
                     stylesController.addRule(rule);
                     });
                     
                var rule = sm.getDominantRuleForElement('div','background-color',true);
                expect(rule).toEqual("#UserContent div#Div_1 { background-color: blue }");

        });

    });

    /* Disable a Rule */
    describe('DisableRule', function() {
    
        it('Rule disabled successfully', function() {
            //Type of rule is not a number
            var rule = sm.disableRule('div#Div_1 { background-color: black }');
            expect(rule).toEqual('div#Div_1'+ sm.CONST.GARBAGE_SELECTOR);

        });

    });

    
    /* Enable a Rule */
    describe('EnableRule', function() {
        it('Rule enabled successfully', function() {

           //Type of rule is not a number
           var rule = sm.enableRule('div#Div_1 { background-color: black }');
           expect(rule.selectorText).toEqual('div#Div_1');

            //Type of rule is a number ie index
            var rule = sm.enableRule('div#Div_1 { background-color: black }');
            expect(rule).toEqual("div#Div_1 { background-color: black }");

        });

    });

    
    /* Set Rule Selector */
    describe('SetRuleSelector', function() {
    
            it('Set Rule Selector successfully', function() {
                var rule = sm.setRuleSelector('div#Div_1 { background-color: black }', 'Div');
                expect(rule).toEqual("Div { background-color: black }");
                expect(rule.specificity.specificity.id).toEqual(0);
                //expect(rule.specificity.specificity.class).toEqual(0);
                expect(rule.specificity.specificity.element).toEqual(1);
            });

    });
    
    
    /*Calculate Specificity */
    describe('Calculate Specificity', function() {
    
            it('Calculated specificity successfully', function() {
                var specificity = sm.calculateSpecificity(0,1);
                expect(specificity.specificity[2]).toEqual(1);
            });

    });

});