xblock_validation_spec.js 6.62 KB
Newer Older
1 2 3 4 5
define(['js/models/xblock_validation'],
    function(XBlockValidationModel) {
        var verifyModel;

        verifyModel = function(model, expected_empty, expected_summary, expected_messages, expected_xblock_id) {
6 7 8 9
            expect(model.get('empty')).toBe(expected_empty);
            expect(model.get('summary')).toEqual(expected_summary);
            expect(model.get('messages')).toEqual(expected_messages);
            expect(model.get('xblock_id')).toBe(expected_xblock_id);
10 11 12 13 14
        };

        describe('XBlockValidationModel', function() {
            it('handles empty variable', function() {
                verifyModel(new XBlockValidationModel({parse: true}), true, {}, [], null);
Eric Fischer committed
15
                verifyModel(new XBlockValidationModel({empty: true}, {parse: true}), true, {}, [], null);
16 17 18 19 20

                // It is assumed that the "empty" state on the JSON object passed in is correct
                // (no attempt is made to correct other variables based on empty==true).
                verifyModel(
                    new XBlockValidationModel(
Eric Fischer committed
21
                        {empty: true, messages: [{text: 'Bad JSON case'}], xblock_id: 'id'},
22 23 24 25
                        {parse: true}
                    ),
                    true,
                    {},
Eric Fischer committed
26
                    [{text: 'Bad JSON case'}], 'id'
27 28 29 30 31 32 33
                );
            });

            it('creates a summary if not defined', function() {
                // Single warning message.
                verifyModel(
                    new XBlockValidationModel({
Eric Fischer committed
34 35
                        empty: false,
                        xblock_id: 'id'
36 37
                    }, {parse: true}),
                    false,
Eric Fischer committed
38
                    {text: 'This component has validation issues.', type: 'warning'},
39
                    [],
40
                    'id'
41 42 43 44
                );
                // Two messages that compute to a "warning" state in the summary.
                verifyModel(
                    new XBlockValidationModel({
Eric Fischer committed
45 46 47
                        empty: false,
                        messages: [{text: 'one', type: 'not-configured'}, {text: 'two', type: 'warning'}],
                        xblock_id: 'id'
48 49
                    }, {parse: true}),
                    false,
Eric Fischer committed
50 51
                    {text: 'This component has validation issues.', type: 'warning'},
                    [{text: 'one', type: 'not-configured'}, {text: 'two', type: 'warning'}],
52
                    'id'
53 54 55 56
                );
                // Two messages, with one of them "error", resulting in an "error" state in the summary.
                verifyModel(
                    new XBlockValidationModel({
Eric Fischer committed
57 58 59
                        empty: false,
                        messages: [{text: 'one', type: 'warning'}, {text: 'two', type: 'error'}],
                        xblock_id: 'id'
60 61
                    }, {parse: true}),
                    false,
Eric Fischer committed
62 63
                    {text: 'This component has validation issues.', type: 'error'},
                    [{text: 'one', type: 'warning'}, {text: 'two', type: 'error'}],
64
                    'id'
65 66 67 68 69 70 71
                );
            });

            it('respects summary properties that are defined', function() {
                // Summary already present (both text and type), no messages.
                verifyModel(
                    new XBlockValidationModel({
Eric Fischer committed
72 73 74
                        empty: false,
                        xblock_id: 'id',
                        summary: {text: 'my summary', type: 'custom type'}
75 76
                    }, {parse: true}),
                    false,
Eric Fischer committed
77
                    {text: 'my summary', type: 'custom type'},
78
                    [],
79
                    'id'
80 81 82 83
                );
                // Summary text present, but not type (will get default value of warning).
                verifyModel(
                    new XBlockValidationModel({
Eric Fischer committed
84 85 86
                        empty: false,
                        xblock_id: 'id',
                        summary: {text: 'my summary'}
87 88
                    }, {parse: true}),
                    false,
Eric Fischer committed
89
                    {text: 'my summary', type: 'warning'},
90
                    [],
91
                    'id'
92 93 94 95
                );
                // Summary type present, but not text.
                verifyModel(
                    new XBlockValidationModel({
Eric Fischer committed
96 97 98 99
                        empty: false,
                        summary: {type: 'custom type'},
                        messages: [{text: 'one', type: 'not-configured'}, {text: 'two', type: 'warning'}],
                        xblock_id: 'id'
100 101
                    }, {parse: true}),
                    false,
Eric Fischer committed
102 103
                    {text: 'This component has validation issues.', type: 'custom type'},
                    [{text: 'one', type: 'not-configured'}, {text: 'two', type: 'warning'}],
104
                    'id'
105 106 107 108
                );
                // Summary text present, type will be computed as error.
                verifyModel(
                    new XBlockValidationModel({
Eric Fischer committed
109 110 111 112
                        empty: false,
                        summary: {text: 'my summary'},
                        messages: [{text: 'one', type: 'warning'}, {text: 'two', type: 'error'}],
                        xblock_id: 'id'
113 114
                    }, {parse: true}),
                    false,
Eric Fischer committed
115 116
                    {text: 'my summary', type: 'error'},
                    [{text: 'one', type: 'warning'}, {text: 'two', type: 'error'}],
117
                    'id'
118 119 120 121 122 123
                );
            });

            it('clears messages if showSummaryOnly is true', function() {
                verifyModel(
                    new XBlockValidationModel({
Eric Fischer committed
124 125 126 127 128
                        empty: false,
                        xblock_id: 'id',
                        summary: {text: 'my summary'},
                        messages: [{text: 'one', type: 'warning'}, {text: 'two', type: 'error'}],
                        showSummaryOnly: true
129 130
                    }, {parse: true}),
                    false,
Eric Fischer committed
131
                    {text: 'my summary', type: 'error'},
132
                    [],
133
                    'id'
134 135 136 137
                );

                verifyModel(
                    new XBlockValidationModel({
Eric Fischer committed
138 139 140 141 142
                        empty: false,
                        xblock_id: 'id',
                        summary: {text: 'my summary'},
                        messages: [{text: 'one', type: 'warning'}, {text: 'two', type: 'error'}],
                        showSummaryOnly: false
143 144
                    }, {parse: true}),
                    false,
Eric Fischer committed
145 146
                    {text: 'my summary', type: 'error'},
                    [{text: 'one', type: 'warning'}, {text: 'two', type: 'error'}],
147
                    'id'
148 149 150 151 152
                );
            });
        });
    }
);