summaryrefslogtreecommitdiff
path: root/test/map.test.js
blob: dff28b00313710984dd2bfa8db74a86b17203a1b (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
/* jshint node: true, unused: false */
/* global describe, it, beforeEach, afterEach */
'use strict';
var assert = require('assert');

var mbgl = require('..');
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');

mkdirp.sync('test/results');

var style = {
    'version': 7,
    'name': 'Empty',
    'sources': {
        'mapbox': {
          'type': 'vector',
          'url': './fixtures/tiles.tilejson',
          'maxzoom': 15
        }
    },
    'layers': [{
        'id': 'background',
        'type': 'background',
        'paint': {
            'background-color': 'white'
        }
    }, {
        'id': 'water',
        'type': 'fill',
        'source': 'mapbox',
        'source-layer': 'water',
        'paint': {
            'fill-color': 'blue'
        }
    }]
};

describe('Map', function() {

    describe('constructor', function() {
        it('must be called with new', function() {
            assert.throws(function() {
                mbgl.Map();
            }, /Use the new operator to create new Map objects/);
        });

        it('should require a FileSource object as first parameter', function() {
            assert.throws(function() {
                new mbgl.Map();
            }, /Requires a FileSource as first argument/);

            assert.throws(function() {
                new mbgl.Map('fileSource');
            }, /Requires a FileSource as first argument/);

            assert.throws(function() {
                new mbgl.Map({});
            }, /Requires a FileSource as first argument/);
        });

        it('should require the FileSource object to have request and cancel methods', function() {
            var fileSource = new mbgl.FileSource();

            assert.throws(function() {
                new mbgl.Map(fileSource);
            }, /FileSource must have a request member function/);

            fileSource.request = 'test';
            assert.throws(function() {
                new mbgl.Map(fileSource);
            }, /FileSource must have a request member function/);

            fileSource.request = function() {};
            assert.throws(function() {
                new mbgl.Map(fileSource);
            }, /FileSource must have a cancel member function/);

            fileSource.cancel = 'test';
            assert.throws(function() {
                new mbgl.Map(fileSource);
            }, /FileSource must have a cancel member function/);

            fileSource.cancel = function() {};
            assert.doesNotThrow(function() {
                new mbgl.Map(fileSource);
            });
        });

    });

    describe('load styles', function() {
        var map;

        var fileSource = new mbgl.FileSource();
        fileSource.request = function() {};
        fileSource.cancel = function() {};

        beforeEach(function() {
            map = new mbgl.Map(fileSource);
        });

        afterEach(function() {
            map = null;
        });

        it('requires a string or object as the first parameter', function() {
            assert.throws(function() {
                map.load();
            }, /Requires a map style as first argument/);

            assert.throws(function() {
                map.load('invalid');
            }, /Expect either an object or array at root/);
        });

        it('accepts an empty stylesheet string', function() {
            map.load('{}');
        });

        it('accepts a JSON stylesheet', function() {
            map.load(style);
        });

        it('accepts a stringified stylesheet', function() {
            map.load(JSON.stringify(style));
        });
    });

    describe('render argument requirements', function() {
        var map;

        var fileSource = new mbgl.FileSource();
        fileSource.request = function(req) {
            fs.readFile(path.join('test', req.url), function(err, data) {
                req.respond(err, { data: data });
                assert.ifError(err);
            });
        };
        fileSource.cancel = function() {};

        beforeEach(function() {
            map = new mbgl.Map(fileSource);
        });

        afterEach(function() {
            map = null;
        });

        it('requires an object as the first parameter', function() {
            assert.throws(function() {
                map.render();
            }, /First argument must be an options object/);

            assert.throws(function() {
                map.render('invalid');
            }, /First argument must be an options object/);
        });

        it('requires a callback as the second parameter', function() {
            assert.throws(function() {
                map.render({});
            }, /Second argument must be a callback function/);

            assert.throws(function() {
                map.render({}, 'invalid');
            }, /Second argument must be a callback function/);
        });

        it('requires a style to be set', function(done) {
            map.render({}, function(err) {
                assert.ok(err);
                assert.equal(err.message, 'Style is not set');
                done();
            });
        });

        it('returns an image', function(done) {
            map.load(style);
            map.render({}, function(err, data) {
                assert.ifError(err);
                fs.writeFileSync('test/results/image.png', data);
                done();
            });
        });
    });

});