mirror of
				https://github.com/zulip/zulip.git
				synced 2025-10-30 19:43:47 +00:00 
			
		
		
		
	We now just use a module._load hook to inject stubs into our code. For conversion purposes I temporarily maintain the API of rewiremock, apart from the enable/disable pieces, but I will make a better wrapper in an upcoming commit. We can detect when rewiremock is called after zrequire now, and I fix all the violations in this commit, mostly by using override. We can also detect when a mock is needlessly created, and I fix all the violations in this commit. The one minor nuisance that this commit introduces is that you can only stub out modules in the Zulip source tree, which is now static/js. This should not really be a problem--there are usually better techniques to deal with third party depenencies. In the prior commit I show a typical workaround, which is to create a one-line wrapper in your test code. It's often the case that you can simply use override(), as well. In passing I kill off `reset_modules`, and I eliminated the second argument to zrequire, which dates back to pre-es6 days.
		
			
				
	
	
		
			106 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			106 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| "use strict";
 | |
| 
 | |
| const {strict: assert} = require("assert");
 | |
| 
 | |
| const {rewiremock, zrequire} = require("../zjsunit/namespace");
 | |
| const {run_test} = require("../zjsunit/test");
 | |
| 
 | |
| const channel = {__esModule: true};
 | |
| rewiremock("../../static/js/channel").with(channel);
 | |
| const widgetize = {__esModule: true};
 | |
| rewiremock("../../static/js/widgetize").with(widgetize);
 | |
| const message_store = {__esModule: true};
 | |
| 
 | |
| rewiremock("../../static/js/message_store").with(message_store);
 | |
| 
 | |
| const submessage = zrequire("submessage");
 | |
| 
 | |
| run_test("get_message_events", () => {
 | |
|     let msg = {};
 | |
| 
 | |
|     assert.equal(submessage.get_message_events(msg), undefined);
 | |
| 
 | |
|     msg = {
 | |
|         submessages: [],
 | |
|     };
 | |
|     assert.equal(submessage.get_message_events(msg), undefined);
 | |
| 
 | |
|     const submessages = [
 | |
|         {id: 222, sender_id: 99, content: "84"},
 | |
|         {id: 9, sender_id: 33, content: "42"},
 | |
|     ];
 | |
| 
 | |
|     msg = {
 | |
|         locally_echoed: true,
 | |
|         submessages,
 | |
|     };
 | |
|     assert.equal(submessage.get_message_events(msg), undefined);
 | |
| 
 | |
|     msg = {
 | |
|         submessages,
 | |
|     };
 | |
|     assert.deepEqual(submessage.get_message_events(msg), [
 | |
|         {sender_id: 33, data: 42},
 | |
|         {sender_id: 99, data: 84},
 | |
|     ]);
 | |
| });
 | |
| 
 | |
| run_test("make_server_callback", () => {
 | |
|     const message_id = 444;
 | |
|     const callback = submessage.make_server_callback(message_id);
 | |
|     let was_posted;
 | |
| 
 | |
|     channel.post = (opts) => {
 | |
|         was_posted = true;
 | |
|         assert.deepEqual(opts, {
 | |
|             url: "/json/submessage",
 | |
|             data: {
 | |
|                 message_id,
 | |
|                 msg_type: "whatever",
 | |
|                 content: '{"foo":32}',
 | |
|             },
 | |
|         });
 | |
|     };
 | |
| 
 | |
|     callback({
 | |
|         msg_type: "whatever",
 | |
|         data: {foo: 32},
 | |
|     });
 | |
| 
 | |
|     assert(was_posted);
 | |
| });
 | |
| 
 | |
| run_test("handle_event", () => {
 | |
|     const message = {
 | |
|         id: 42,
 | |
|     };
 | |
| 
 | |
|     const event = {
 | |
|         id: 11,
 | |
|         msg_type: "widget",
 | |
|         sender_id: 99,
 | |
|         message_id: message.id,
 | |
|         content: '"some_data"',
 | |
|     };
 | |
| 
 | |
|     let args;
 | |
|     widgetize.handle_event = (opts) => {
 | |
|         args = opts;
 | |
|     };
 | |
| 
 | |
|     message_store.get = (msg_id) => {
 | |
|         assert.equal(msg_id, message.id);
 | |
|         return message;
 | |
|     };
 | |
| 
 | |
|     submessage.handle_event(event);
 | |
| 
 | |
|     assert.deepEqual(args, {
 | |
|         sender_id: 99,
 | |
|         message_id: 42,
 | |
|         data: "some_data",
 | |
|     });
 | |
| 
 | |
|     assert.deepEqual(message.submessages[0], event);
 | |
| });
 |