xblock_validation_spec.js 6.83 KB
Newer Older
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 144 145 146 147 148 149 150 151 152
define(['js/models/xblock_validation'],
    function(XBlockValidationModel) {
        var verifyModel;

        verifyModel = function(model, expected_empty, expected_summary, expected_messages, expected_xblock_id) {
            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);
        };

        describe('XBlockValidationModel', function() {
            it('handles empty variable', function() {
                verifyModel(new XBlockValidationModel({parse: true}), true, {}, [], null);
                verifyModel(new XBlockValidationModel({"empty": true}, {parse: true}), true, {}, [], null);

                // 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(
                        {"empty": true, "messages": [{"text": "Bad JSON case"}], "xblock_id": "id"},
                        {parse: true}
                    ),
                    true,
                    {},
                    [{"text": "Bad JSON case"}], "id"
                );
            });

            it('creates a summary if not defined', function() {
                // Single warning message.
                verifyModel(
                    new XBlockValidationModel({
                        "empty": false,
                        "xblock_id": "id"
                    }, {parse: true}),
                    false,
                    {"text": "This component has validation issues.", "type": "warning"},
                    [],
                    "id"
                );
                // Two messages that compute to a "warning" state in the summary.
                verifyModel(
                    new XBlockValidationModel({
                        "empty": false,
                        "messages": [{"text": "one", "type": "not-configured"}, {"text": "two", "type": "warning"}],
                        "xblock_id": "id"
                    }, {parse: true}),
                    false,
                    {"text": "This component has validation issues.", "type": "warning"},
                    [{"text": "one", "type": "not-configured"}, {"text": "two", "type": "warning"}],
                    "id"
                );
                // Two messages, with one of them "error", resulting in an "error" state in the summary.
                verifyModel(
                    new XBlockValidationModel({
                        "empty": false,
                        "messages": [{"text": "one", "type": "warning"}, {"text": "two", "type": "error"}],
                        "xblock_id": "id"
                    }, {parse: true}),
                    false,
                    {"text": "This component has validation issues.", "type": "error"},
                    [{"text": "one", "type": "warning"}, {"text": "two", "type": "error"}],
                    "id"
                );
            });

            it('respects summary properties that are defined', function() {
                // Summary already present (both text and type), no messages.
                verifyModel(
                    new XBlockValidationModel({
                        "empty": false,
                        "xblock_id": "id",
                        "summary": {"text": "my summary", "type": "custom type"}
                    }, {parse: true}),
                    false,
                    {"text": "my summary", "type": "custom type"},
                    [],
                    "id"
                );
                // Summary text present, but not type (will get default value of warning).
                verifyModel(
                    new XBlockValidationModel({
                        "empty": false,
                        "xblock_id": "id",
                        "summary": {"text": "my summary"}
                    }, {parse: true}),
                    false,
                    {"text": "my summary", "type": "warning"},
                    [],
                    "id"
                );
                // Summary type present, but not text.
                verifyModel(
                    new XBlockValidationModel({
                        "empty": false,
                        "summary": {"type": "custom type"},
                        "messages": [{"text": "one", "type": "not-configured"}, {"text": "two", "type": "warning"}],
                        "xblock_id": "id"
                    }, {parse: true}),
                    false,
                    {"text": "This component has validation issues.", "type": "custom type"},
                    [{"text": "one", "type": "not-configured"}, {"text": "two", "type": "warning"}],
                    "id"
                );
                // Summary text present, type will be computed as error.
                verifyModel(
                    new XBlockValidationModel({
                        "empty": false,
                        "summary": {"text": "my summary"},
                        "messages": [{"text": "one", "type": "warning"}, {"text": "two", "type": "error"}],
                        "xblock_id": "id"
                    }, {parse: true}),
                    false,
                    {"text": "my summary", "type": "error"},
                    [{"text": "one", "type": "warning"}, {"text": "two", "type": "error"}],
                    "id"
                );
            });

            it('clears messages if showSummaryOnly is true', function() {
                verifyModel(
                    new XBlockValidationModel({
                        "empty": false,
                        "xblock_id": "id",
                        "summary": {"text": "my summary"},
                        "messages": [{"text": "one", "type": "warning"}, {"text": "two", "type": "error"}],
                        "showSummaryOnly": true
                    }, {parse: true}),
                    false,
                    {"text": "my summary", "type": "error"},
                    [],
                    "id"
                );

                verifyModel(
                    new XBlockValidationModel({
                        "empty": false,
                        "xblock_id": "id",
                        "summary": {"text": "my summary"},
                        "messages": [{"text": "one", "type": "warning"}, {"text": "two", "type": "error"}],
                        "showSummaryOnly": false
                    }, {parse: true}),
                    false,
                    {"text": "my summary", "type": "error"},
                    [{"text": "one", "type": "warning"}, {"text": "two", "type": "error"}],
                    "id"
                );
            });
        });
    }
);