summaryrefslogtreecommitdiff
path: root/test/wikipedia/models/testArticleModel.js
blob: f916369572d730ff04f74699547270ce2dee491c (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
const ArticleModel = imports.wikipedia.models.article_model;

describe("Wikipedia article model", function() {
    let mockJsonData = {
        title: 'Article Title',
        url: 'file:///',
        source: 'Mock data',
        categories: [
            'Category One',
            'Category Two'
        ]
    };

    describe("from JSON", function() {
        let model;

        beforeEach(function() {
            model = ArticleModel.newFromJson(mockJsonData);
        });

        it("has an article title", function() {
            expect(model.title).toEqual(mockJsonData.title);
        });

        it("has a uri", function() {
            expect(model.uri).toEqual(mockJsonData.url);
        });

        it("has a list of categories", function() {
            expect(model.getCategories()).toEqual(mockJsonData.categories);
        });
    });

    describe("from properties", function() {
        let model;
        beforeEach(function() {
            model = new ArticleModel.ArticleModel({
                title: 'Article Title',
                uri: 'file://'
            });
        });

        it("is an instance of an ArticleModel", function() {
            expect(model instanceof ArticleModel.ArticleModel).toBeTruthy();
        });

        it("has a title", function() {
            expect(model.title).toEqual('Article Title');
        });

        it("has a URI", function() {
            expect(model.uri).toEqual('file://');
        });

        it("has no categories", function() {
            expect(model.getCategories().length).toEqual(0);
        });
    });

    describe("setCategories method", function() {
        let model;

        beforeEach(function() {
            model = new ArticleModel.ArticleModel();
        });

        it("adds categories", function() {
            let expectedCategories = ['One', 'Two', 'Three'];
            model.setCategories(expectedCategories);
            expect(model.getCategories()).toEqual(expectedCategories);
        });

        it("replaces existing categories", function() {
            model.setCategories(['One', 'Two']);
            let expectedCategories = ['One', 'Two', 'Three'];
            model.setCategories(expectedCategories);
            expect(model.getCategories()).toEqual(expectedCategories);
        });
    });

    it("appends new categories on addCategory", function() {
        let model = new ArticleModel.ArticleModel();

        model.addCategory('One');
        model.addCategory('Two');
        model.addCategory('Three');
        expect(model.getCategories()).toEqual(['One', 'Two', 'Three']);
    });
    describe("hasCategory method", function() {
        let model;
        let expectedCategories = ['One', 'Two', 'Three'];
 
        beforeEach(function() {
            model = new ArticleModel.ArticleModel;
            model.setCategories(expectedCategories);
        });

        expectedCategories.forEach(function(category) {
            (function(categoryName) {
                 it("returns true for category named " + categoryName, function() {
                     expect(model.hasCategory(categoryName)).toBeTruthy();
                 });
             });
        });

        it("returns false for an unexpected category", function() {
            expect(model.hasCategory('unexpected')).toBeFalsy();
        });
    });
});