summaryrefslogtreecommitdiff
path: root/test/wikipedia/models/testDomainWikiModel.js
blob: f30d1bd49ed153ec637ee3371c8b87b162cc9425 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const DomainWikiModel = imports.wikipedia.models.domain_wiki_model;

describe('Domain Wiki Model', function () {
    const mockJsonData = {
        categories: [
            {
                category_name: 'Main Category',
                content_text: 'Lorem Ipsum',
                image_file: 'file:///image.jpg',
                image_thumb_uri: 'file:///image_thumb.jpg',
                is_main_category: true,
                subcategories: [
                    'Category One',
                    'Category Two'
                ]
            },
            {
                category_name: 'Category One',
                content_text: 'Lorem Ipsum',
                image_file: 'file:///image.jpg',
                image_thumb_uri: 'file:///image_thumb.jpg',
                is_main_category: false,
                subcategories: []
            },
            {
                category_name: 'Category Two',
                content_text: 'Lorem Ipsum',
                image_file: 'file:///image.jpg',
                image_thumb_uri: 'file:///image_thumb.jpg',
                is_main_category: false,
                subcategories: [
                    'Category Three'
                ]
            },
            {
                category_name: 'Category Three',
                content_text: 'Lorem Ipsum',
                image_file: 'file:///image.jpg',
                image_thumb_uri: 'file:///image_thumb.jpg',
                is_main_category: false,
                subcategories: []
            }
        ],
        articles: [
            {
                title: 'Article One',
                url: 'file:///article1.html',
                source: 'Mock data',
                categories: [
                    'Category One'
                ]
            },
            {
                title: 'Article Two',
                url: 'file:///article2.html',
                source: 'Mock data',
                categories: [
                    'Category One',
                    'Category Two'
                ]
            },
            {
                title: 'Article Three',
                url: 'file:///article3.html',
                source: 'Mock data',
                categories: [
                    'Category Two'
                ]
            }
        ]
    };
    beforeEach(function () {
        let model = new DomainWikiModel.DomainWikiModel();

        jasmine.addMatchers({
            toHaveObjectsContainingProperties: function () {
                return {
                    compare: function (actual, propertyMap) {
                        let result = {
                            pass: (function () {
                                for (let property in propertyMap) {
                                    let allValuesListedHaveAMatchForObject = actual.some(function (object) {
                                        if (object[property] == 'undefined') {
                                            return false;
                                        }

                                        let propertyValueMatchedForObject =
                                            propertyMap[property].some(function (value) {
                                                return object[property] == value;
                                            });

                                        return propertyValueMatchedForObject;
                                    });

                                    if (!allValuesListedHaveAMatchForObject)
                                        return false;
                                }

                                return true;
                            })(),

                            message: (function () {
                                let msg = 'Expected objects to have the following values for the following properties \n';
                                for (let property in propertyMap) {
                                    msg += ' - Property: ' + property + '\n';
                                    for (let value in propertyMap[property]) {
                                        msg += '     * Value: ' + propertyMap[property][value].toString() + '\n';
                                    }
                                }

                                msg += 'Object actually has the following toplevel properties\n';

                                for (let i = 0; i < actual.length; i++) {
                                    let object = actual[i];
                                    msg += ' Object in position ' + i + '\n';
                                    for (let property in object) {
                                        msg += ' - ' + property + ' : ' + object[property] + '\n';
                                    }
                                }

                                return msg;
                            })()
                        };

                        return result;
                    }
                };
            }
        });
    });

    describe('when loaded from some mock JSON data', function () {
        let model;
        beforeEach(function () {
            model = new DomainWikiModel.DomainWikiModel();
            model.loadFromJson(mockJsonData);
        });

        it('returns all articles when getting articles', function () {
            let articles = model.getArticles();
            expect(articles).toHaveObjectsContainingProperties({
                title: [ 'Article One', 'Article Two', 'Article Three' ]
            });
        });

        it('can get articles for a category', function () {
            let articles = model.getArticlesForCategory('Category One');
            expect(articles).toHaveObjectsContainingProperties({
                title: [ 'Article One', 'Article Two' ]
            });
        });

        it('has no articles on a category that does not have articles', function () {
            let articles = model.getArticlesForCategory('Main Category');
            expect(articles.length).toEqual(0);
        });

        it('has no articles for a category that does not exist', function () {
            let articles = model.getArticlesForCategory('Nonexistent');
            expect(articles.length).toEqual(0);
        });

        it('can check whether or not a category has articles', function () {
            expect(model._getCategoryHasArticles('Category Two')).toBeTruthy();
        });

        it('can check whether or not a category does not have articles', function () {
            expect(model._getCategoryHasArticles('Category Three')).toBeFalsy();
        });

        it('verifies that a category that does not exist has no articles', function () {
            expect(model._getCategoryHasArticles('Nonexistent')).toBeFalsy();
        });

        describe('category fetch', function () {
            let category;

            beforeEach(function () {
                category = model.getCategory('Category One');
            });

            it('actually returns a category', function () {
                expect(category.__name__).toEqual('CategoryModel');
            });

            it('returns the right category', function () {
                expect(category.title).toEqual('Category One');
            });
        });

        it("returns an undefined value if we try to get a category that doesn't exist", function () {
            expect(model.getCategory('Nonexistent')).toBeUndefined();
        });

        it("returns 'Main Category' when getting the main category", function () {
            let category = model.getMainCategory();

            expect(category).toEqual(new jasmine.ObjectContaining({
                'title' : 'Main Category'
            }));
        });
    });

    it('returns null when the Main Category is unset', function () {
        let model = new DomainWikiModel.DomainWikiModel();
        expect(model.getMainCategory()).toBeNull();
    });
});