Mercurial > bugzilla-dashboard
comparison js/modules/mocks.js @ 100:c486d35fad27
added more mocks; black-box now talks to a really simple fake bugzilla 'server'.
author | Atul Varma <avarma@mozilla.com> |
---|---|
date | Tue, 27 Apr 2010 23:06:29 -0700 |
parents | 544d339d2b4c |
children | 00b02ba5236c |
comparison
equal
deleted
inserted
replaced
99:544d339d2b4c | 100:c486d35fad27 |
---|---|
1 Require.modules["mocks/cache"] = function(exports, require) { | 1 Require.modules["mocks/cache"] = function(exports, require) { |
2 function copy(obj) { | 2 function copy(obj) { |
3 return JSON.parse(JSON.stringify(obj)); | 3 if (typeof(obj) == "function") |
4 throw new Error("can't store functions"); | |
5 if (typeof(obj) == "object") | |
6 return JSON.parse(JSON.stringify(obj)); | |
7 return obj; | |
4 } | 8 } |
5 | 9 |
6 function MockCache() { | 10 function MockCache(delegate) { |
11 this.delegate = delegate; | |
7 this.cache = {}; | 12 this.cache = {}; |
8 }; | 13 }; |
9 | 14 |
10 MockCache.prototype = { | 15 MockCache.prototype = { |
11 get: function get(key) { | 16 get: function get(key) { |
12 console.log("cache get", key); | 17 this.delegate("get", [key]); |
13 if (key in this.cache) | 18 if (key in this.cache) |
14 return copy(this.cache[key]); | 19 return copy(this.cache[key]); |
15 return null; | 20 return null; |
16 }, | 21 }, |
17 set: function set(key, value) { | 22 set: function set(key, value) { |
18 console.log("cache set", key); | 23 this.delegate("set", [key, value]); |
19 this.cache[key] = copy(value); | 24 this.cache[key] = copy(value); |
20 }, | 25 }, |
21 clear: function clear(key, value) { | 26 clear: function clear() { |
27 this.delegate("clear", []); | |
22 this.cache = {}; | 28 this.cache = {}; |
23 } | 29 } |
24 }; | 30 }; |
25 | 31 |
26 exports.create = function create() { | 32 exports.create = function create(delegate) { |
27 return new MockCache(); | 33 return new MockCache(delegate); |
34 }; | |
35 }; | |
36 | |
37 Require.modules["mocks/bugzilla/trivial"] = function(exports, require) { | |
38 var bug = { | |
39 'summary': 'Application destroys computer on startup', | |
40 'last_change_time': '2010-04-13T18:02:00Z', | |
41 'status': 'NEW', | |
42 'priority': 'P1', | |
43 'severity': 'blocker', | |
44 'id': '558680' | |
45 }; | |
46 | |
47 var user = { | |
48 'email': 'john@doe.com', | |
49 'real_name': 'John Doe', | |
50 'name': 'john@doe.com' | |
51 }; | |
52 | |
53 var config = { | |
54 product: { | |
55 foo: { | |
56 component: { | |
57 caching: {}, | |
58 passwords: {} | |
59 } | |
60 }, | |
61 bar: { | |
62 component: { | |
63 "help system": {}, | |
64 "curmudgeonry": {} | |
65 } | |
66 } | |
67 } | |
68 }; | |
69 | |
70 exports.makeAjaxImpl = function makeAjaxImpl(delegate, setTimeout) { | |
71 return function ajaxImpl(options) { | |
72 var authenticated = false; | |
73 if (options.data && options.data.username) { | |
74 if (!(options.data.username == 'john@doe.com' && | |
75 options.data.password == 'test')) | |
76 return {error: true, message: "wrong password, yo!"}; | |
77 authenticated = true; | |
78 } | |
79 switch (options.url) { | |
80 case "/bug": | |
81 if (!('resolution' in options.data)) | |
82 return {bugs: [bug]}; | |
83 return {bugs: []}; | |
84 case "/configuration": | |
85 return config; | |
86 case "/user": | |
87 if (!authenticated) | |
88 return {error: true, message: "needs login, yo!"}; | |
89 if (user.email.indexOf(options.data.match) != -1 || | |
90 user.real_name.indexOf(options.data.match) != -1) | |
91 return {users: [user]}; | |
92 return {users: []}; | |
93 default: | |
94 throw new Error("unexpected url: " + options.url); | |
95 } | |
96 }; | |
28 }; | 97 }; |
29 }; | 98 }; |
30 | 99 |
31 Require.modules["mocks/bugzilla"] = function(exports, require) { | 100 Require.modules["mocks/bugzilla"] = function(exports, require) { |
32 exports.create = function create(Bugzilla) { | 101 const DEFAULT_RESPONSE_TIME = 500; |
102 | |
103 function response(delegate, obj, time) { | |
104 if (time === undefined) | |
105 time = DEFAULT_RESPONSE_TIME; | |
106 | |
107 function xhrDelegate(method, args) { | |
108 delegate("xhr." + method, args); | |
109 } | |
110 | |
111 var req = require("mocks/xhr").create(xhrDelegate); | |
112 | |
113 require("window").setTimeout( | |
114 function() { | |
115 req.responseText = JSON.stringify(obj); | |
116 req.status = 200; | |
117 req.statusText = "OK"; | |
118 req.mockTriggerEvent({type: "load", target: req}); | |
119 }, | |
120 time | |
121 ); | |
122 return req; | |
123 } | |
124 | |
125 exports.create = function create(Bugzilla, ajaxImpl, delegate) { | |
33 function MockBugzilla() { | 126 function MockBugzilla() { |
34 this.ajax = function ajax(options) { | 127 this.ajax = function ajax(options) { |
35 console.log(options); | 128 var obj = ajaxImpl(options, exports); |
36 throw new Error("MockBugzilla.ajax() not implemented"); | 129 var req = response(delegate, obj); |
130 req.addEventListener( | |
131 "load", | |
132 function onLoad() { | |
133 var response = JSON.parse(req.responseText); | |
134 if (!response.error) | |
135 options.success(response); | |
136 }, | |
137 false | |
138 ); | |
139 return req; | |
37 }; | 140 }; |
38 }; | 141 }; |
39 | 142 |
40 MockBugzilla.prototype = Bugzilla; | 143 MockBugzilla.prototype = Bugzilla; |
41 | 144 |
86 delegate("open", [method, url]); | 189 delegate("open", [method, url]); |
87 }; | 190 }; |
88 | 191 |
89 self.mockTriggerEvent = function(event) { | 192 self.mockTriggerEvent = function(event) { |
90 verifyEventType(event.type); | 193 verifyEventType(event.type); |
91 listeners.forEach( | 194 listeners[event.type].forEach( |
92 function(listener) { | 195 function(listener) { |
93 listener(event); | 196 listener(event); |
94 }); | 197 }); |
95 }; | 198 }; |
96 } | 199 } |