UNPKG

4.15 kBJavaScriptView Raw
1var _ = require('lodash'),
2 __j = require('jasmine'),
3 CR = require('ConsoleReporter'),
4 describe = __j.describe,
5 it = __j.it,
6 expect = __j.expect,
7 jasmine = __j.jasmine,
8 beforeEach = __j.beforeEach;
9
10// These are in alphabetical order. It's not an accident. Keep it that way.
11var apiChecks = {
12 'Ti.UI.Button': function(o) {
13 expect(o).toHaveFunction('setTitle');
14 },
15 'Ti.UI.ImageView': function(o) {
16 expect(o).toHaveFunction('pause');
17 },
18 'Ti.UI.iOS.CoverFlowView': function(o) {
19 expect(o).toHaveFunction('getImages');
20 },
21 'Ti.UI.iPhone.NavigationGroup': function(o) {
22 apiChecks['Ti.UI.Window'](o.window);
23 },
24 'Ti.UI.Label': function(o) {
25 expect(o).toHaveFunction('setText');
26 },
27 'Ti.UI.ScrollView': function(o) {
28 expect(o).toHaveFunction('scrollTo');
29 },
30 'Ti.UI.Slider': function(o) {
31 if (OS_IOS) {
32 expect(o).toHaveFunction('getThumbImage');
33 } else {
34 expect(o).toHaveFunction('getMinRange');
35 }
36 },
37 'Ti.UI.Tab': function(o) {
38 apiChecks['Ti.UI.Window'](o.window);
39 },
40 'Ti.UI.TabGroup': function(o) {
41 expect(o).toHaveFunction('addTab');
42 },
43 'Ti.UI.TableView': function(o) {
44 expect(o).toHaveFunction('appendRow');
45 },
46 'Ti.UI.TableViewRow': function(o) {
47 expect(o).toHaveFunction('getClassName');
48 },
49 'Ti.UI.Window': function(o) {
50 expect(o).toHaveFunction('open');
51 }
52};
53
54function sortAndStringify(obj) {
55 return JSON.stringify(obj, function(k,v) {
56 if (_.isObject(v) && !_.isArray(v) && !_.isFunction(v)) {
57 return sortObject(v);
58 }
59 return v;
60 });
61}
62
63function sortObject(o) {
64 var sorted = {},
65 key, a = [];
66
67 for (key in o) {
68 if (o.hasOwnProperty(key)) {
69 a.push(key);
70 }
71 }
72
73 a.sort();
74
75 for (key = 0; key < a.length; key++) {
76 sorted[a[key]] = o[a[key]];
77 }
78 return sorted;
79}
80
81function addMatchers() {
82 beforeEach(function() {
83 this.addMatchers({
84 toBeTiProxy: function() {
85 return _.isFunction(this.actual.addEventListener);
86 },
87 toBeController: function() {
88 return this.actual.__iamalloy === true;
89 },
90 toBeWidget: function() {
91 return this.actual.__iamalloy === true;
92 },
93 toContainSameAs: function(array) {
94 var actual = this.actual;
95 this.message = function() {
96 return 'expected ' + sortAndStringify(actual) + ' to contain ' +
97 'same elements as ' + sortAndStringify(array);
98 };
99
100 return sortAndStringify(actual) === sortAndStringify(array);
101 },
102 toHaveStyle: function(style) {
103 var component = this.actual;
104 var obj = {};
105 _.each(style, function(v,k) {
106 obj[k] = component[k];
107 });
108
109 this.message = function() {
110 return 'expected ' + this.actual.toString() + ' to have style:\n' +
111 sortAndStringify(style) + '\nbut found this instead:\n' +
112 sortAndStringify(obj);
113 };
114
115 return sortAndStringify(obj) === sortAndStringify(style);
116 },
117 toHaveFunction: function(func) {
118 return _.isFunction(this.actual[func]);
119 },
120 toBeFile: function() {
121 var file = _.isString(this.actual) ? Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, this.actual) : this.actual;
122 return file.exists() && file.isFile();
123 }
124 });
125 });
126}
127
128function validateUiComponent($, id, opts) {
129 if (!id) { throw('validateUiComponent exception: No id given'); }
130
131 var comp = $[id];
132 it('#' + id + ' is defined', function() {
133 expect(comp).toBeDefined();
134 expect(comp).not.toBeNull();
135 });
136
137 it('#' + id + ' is a Titanium proxy object', function() {
138 expect(comp).toBeTiProxy();
139 });
140
141 if (opts.api && apiChecks[opts.api]) {
142 it('#' + id + ' component is a ' + opts.api, function() {
143 apiChecks[opts.api](comp);
144 });
145 }
146
147 if (opts.style) {
148 if ($.__styler && $.__styler[id] && !_.isEmpty($.__styler[id])) {
149 opts.style = _.extend(opts.style, $.__styler[id]);
150 }
151 it('#' + id + ' component has correct style', function() {
152 expect(comp).toHaveStyle(opts.style);
153 });
154 }
155}
156
157function launchTests() {
158 jasmine.getEnv().addReporter(new CR({
159 doneCallback: function(runner) {
160 alert(runner.specs().length + ' specs, ' + runner.results().failedCount + ' failed');
161 }
162 }));
163 jasmine.getEnv().execute();
164}