How to use fake method in sinon

Best JavaScript code snippet using sinon

tumblrTriggerSpec.js

Source:tumblrTriggerSpec.js Github

copy

Full Screen

1var TumblrTrigger = require('../lib/triggers/tumblrTrigger.js');2describe("TumblrTrigger", function() {3 var fakeBot;4 beforeEach(function() {5 fakeBot = jasmine.createSpyObj('fakeBot', ['sendMessage']);6 });7 it("should not post on demand if there is nothing after a command", function() {8 var fakeTumblrClient = jasmine.createSpyObj('fakeTumblrClient', ['text', 'photo']);9 var trigger = TumblrTrigger.create("tumblrTrigger", fakeBot, { blogName: 'blogname', autoPost: true, autoPostContext: true, tumblr: fakeTumblrClient } );10 expect(trigger.onChatMessage('roomId', 'userId', '!posttext', false, false)).toEqual(false);11 expect(trigger.onChatMessage('roomId', 'userId', '!posttext ', false, false)).toEqual(false);12 expect(trigger.onChatMessage('roomId', 'userId', '!postphoto ', false, false)).toEqual(false);13 expect(fakeTumblrClient.text).not.toHaveBeenCalled();14 expect(fakeTumblrClient.photo).not.toHaveBeenCalled();15 });16 it("should not post on demand without the right command", function() {17 var fakeTumblrClient = jasmine.createSpyObj('fakeTumblrClient', ['text']);18 var trigger = TumblrTrigger.create("tumblrTrigger", fakeBot, { blogName: 'blogname', autoPost: true, autoPostContext: true, tumblr: fakeTumblrClient } );19 expect(trigger.onChatMessage('roomId', 'userId', '', false, false)).toEqual(false);20 expect(trigger.onChatMessage('roomId', 'userId', 'posttext', false, false)).toEqual(false);21 expect(trigger.onChatMessage('roomId', 'userId', 'posttext text here', false, false)).toEqual(false);22 expect(trigger.onChatMessage('roomId', 'userId', ' !posttext text here', false, false)).toEqual(false);23 expect(fakeTumblrClient.text).not.toHaveBeenCalled();24 expect(fakeTumblrClient.text).not.toHaveBeenCalled();25 });26 it("should not post on demand from a friend message", function() {27 var fakeTumblrClient = jasmine.createSpyObj('fakeTumblrClient', ['text']);28 var trigger = TumblrTrigger.create("tumblrTrigger", fakeBot, { blogName: 'blogname', autoPost: true, autoPostContext: true, tumblr: fakeTumblrClient } );29 expect(trigger.onFriendMessage('userId', '!posttext text here', false)).toEqual(false);30 expect(fakeTumblrClient.text).not.toHaveBeenCalled();31 });32 it("should post text on demand", function() {33 var fakeTumblrClient = jasmine.createSpyObj('fakeTumblrClient', ['text']);34 var trigger = TumblrTrigger.create("tumblrTrigger", fakeBot, { blogName: 'blogname', autoPost: true, autoPostContext: true, tumblr: fakeTumblrClient } );35 expect(trigger.onChatMessage('roomId', 'userId', '!posttext text here', false, false)).toEqual(true);36 expect(fakeTumblrClient.text.calls.length).toEqual(1);37 expect(fakeTumblrClient.text.calls[0].args[0]).toEqual('blogname');38 expect(fakeTumblrClient.text.calls[0].args[1]).toEqual({ body: 'text here', tags: 'text' });39 });40 it("should post a link as a photo on demand", function() {41 var fakeTumblrClient = jasmine.createSpyObj('fakeTumblrClient', ['photo']);42 var trigger = TumblrTrigger.create("tumblrTrigger", fakeBot, { blogName: 'blogname', autoPost: true, autoPostContext: true, tumblr: fakeTumblrClient } );43 expect(trigger.onChatMessage('roomId', 'userId', '!postphoto http://a.com/image.jpg', false, false)).toEqual(true);44 expect(fakeTumblrClient.photo.calls.length).toEqual(1);45 expect(fakeTumblrClient.photo.calls[0].args[0]).toEqual('blogname');46 expect(fakeTumblrClient.photo.calls[0].args[1]).toEqual({ tags: 'photo', source: 'http://a.com/image.jpg', link: 'http://a.com/image.jpg' });47 expect(trigger.onChatMessage('roomId', 'userId', '!postphoto http://a.com/image.jpg ', false, false)).toEqual(true);48 expect(fakeTumblrClient.photo.calls.length).toEqual(2);49 expect(fakeTumblrClient.photo.calls[1].args[0]).toEqual('blogname');50 expect(fakeTumblrClient.photo.calls[1].args[1]).toEqual({ tags: 'photo', source: 'http://a.com/image.jpg', link: 'http://a.com/image.jpg' });51 expect(trigger.onChatMessage('roomId', 'userId', '!postphoto http://a.com', false, false)).toEqual(true);52 expect(fakeTumblrClient.photo.calls.length).toEqual(3);53 expect(fakeTumblrClient.photo.calls[2].args[0]).toEqual('blogname');54 expect(fakeTumblrClient.photo.calls[2].args[1]).toEqual({ tags: 'photo', source: 'http://a.com', link: 'http://a.com' });55 expect(trigger.onChatMessage('roomId', 'userId', '!postphoto http://a.com/image.bitmap', false, false)).toEqual(true);56 expect(fakeTumblrClient.photo.calls.length).toEqual(4);57 expect(fakeTumblrClient.photo.calls[3].args[0]).toEqual('blogname');58 expect(fakeTumblrClient.photo.calls[3].args[1]).toEqual({ tags: 'photo', source: 'http://a.com/image.bitmap', link: 'http://a.com/image.bitmap' });59 expect(trigger.onChatMessage('roomId', 'userId', '!postphoto http://a.com/image.jpg?test=param#anchor', false, false)).toEqual(true);60 expect(fakeTumblrClient.photo.calls.length).toEqual(5);61 expect(fakeTumblrClient.photo.calls[4].args[0]).toEqual('blogname');62 expect(fakeTumblrClient.photo.calls[4].args[1]).toEqual({ tags: 'photo', source: 'http://a.com/image.jpg?test=param#anchor', link: 'http://a.com/image.jpg?test=param#anchor' });63 expect(trigger.onChatMessage('roomId', 'userId', '!postphoto http://a.com/image.JPG', false, false)).toEqual(true);64 expect(fakeTumblrClient.photo.calls.length).toEqual(6);65 expect(fakeTumblrClient.photo.calls[5].args[0]).toEqual('blogname');66 expect(fakeTumblrClient.photo.calls[5].args[1]).toEqual({ tags: 'photo', source: 'http://a.com/image.JPG', link: 'http://a.com/image.JPG' });67 });68 it("should post a photo on demand with additional context if provided", function() {69 var fakeTumblrClient = jasmine.createSpyObj('fakeTumblrClient', ['photo']);70 var trigger = TumblrTrigger.create("tumblrTrigger", fakeBot, { blogName: 'blogname', autoPost: true, autoPostContext: true, tumblr: fakeTumblrClient } );71 expect(trigger.onChatMessage('roomId', 'userId', '!postphoto check this out http://a.com/image.jpg', false, false)).toEqual(true);72 expect(fakeTumblrClient.photo.calls.length).toEqual(1);73 expect(fakeTumblrClient.photo.calls[0].args[0]).toEqual('blogname');74 expect(fakeTumblrClient.photo.calls[0].args[1]).toEqual({ tags: 'photo', caption: 'check this out http://a.com/image.jpg', source: 'http://a.com/image.jpg', link: 'http://a.com/image.jpg' });75 expect(trigger.onChatMessage('roomId', 'userId', '!postphoto http://a.com/image.jpg check this out', false, false)).toEqual(true);76 expect(fakeTumblrClient.photo.calls.length).toEqual(2);77 expect(fakeTumblrClient.photo.calls[1].args[0]).toEqual('blogname');78 expect(fakeTumblrClient.photo.calls[1].args[1]).toEqual({ tags: 'photo', caption: 'http://a.com/image.jpg check this out', source: 'http://a.com/image.jpg', link: 'http://a.com/image.jpg' });79 expect(trigger.onChatMessage('roomId', 'userId', '!postphoto check this http://a.com/image.jpg out', false, false)).toEqual(true);80 expect(fakeTumblrClient.photo.calls.length).toEqual(3);81 expect(fakeTumblrClient.photo.calls[2].args[0]).toEqual('blogname');82 expect(fakeTumblrClient.photo.calls[2].args[1]).toEqual({ tags: 'photo', caption: 'check this http://a.com/image.jpg out', source: 'http://a.com/image.jpg', link: 'http://a.com/image.jpg' });83 });84 it("should not post a photo on demand if no links are provided", function() {85 var fakeTumblrClient = jasmine.createSpyObj('fakeTumblrClient', ['photo']);86 var trigger = TumblrTrigger.create("tumblrTrigger", fakeBot, { blogName: 'blogname', autoPost: true, autoPostContext: true, tumblr: fakeTumblrClient } );87 expect(trigger.onChatMessage('roomId', 'userId', '!postphoto no links here http://', false, false)).toEqual(false);88 expect(fakeTumblrClient.photo).not.toHaveBeenCalled();89 });90 it("should post a quote on demand", function() {91 var fakeTumblrClient = jasmine.createSpyObj('fakeTumblrClient', ['quote']);92 var trigger = TumblrTrigger.create("tumblrTrigger", fakeBot, { blogName: 'blogname', autoPost: true, autoPostContext: true, tumblr: fakeTumblrClient } );93 var quote = 'this is a quote';94 expect(trigger.onChatMessage('roomId', 'userId', '!postquote ' + quote, false, false)).toEqual(true);95 expect(fakeTumblrClient.quote.calls.length).toEqual(1);96 expect(fakeTumblrClient.quote.calls[0].args[0]).toEqual('blogname');97 expect(fakeTumblrClient.quote.calls[0].args[1]).toEqual({ quote: quote, tags: 'quote' });98 });99 it("should post a link on demand", function() {100 var fakeTumblrClient = jasmine.createSpyObj('fakeTumblrClient', ['link']);101 var trigger = TumblrTrigger.create("tumblrTrigger", fakeBot, { blogName: 'blogname', autoPost: true, autoPostContext: true, tumblr: fakeTumblrClient } );102 expect(trigger.onChatMessage('roomId', 'userId', '!postlink http://a.com', false, false)).toEqual(true);103 expect(fakeTumblrClient.link.calls.length).toEqual(1);104 expect(fakeTumblrClient.link.calls[0].args[0]).toEqual('blogname');105 expect(fakeTumblrClient.link.calls[0].args[1]).toEqual({ tags: 'link', url: 'http://a.com' });106 expect(trigger.onChatMessage('roomId', 'userId', '!postlink http://a.com/link?test=param#anchor', false, false)).toEqual(true);107 expect(fakeTumblrClient.link.calls.length).toEqual(2);108 expect(fakeTumblrClient.link.calls[1].args[0]).toEqual('blogname');109 expect(fakeTumblrClient.link.calls[1].args[1]).toEqual({ tags: 'link', url: 'http://a.com/link?test=param#anchor' });110 expect(trigger.onChatMessage('roomId', 'userId', '!postlink http://a.com/link x', false, false)).toEqual(true);111 expect(fakeTumblrClient.link.calls.length).toEqual(3);112 expect(fakeTumblrClient.link.calls[2].args[0]).toEqual('blogname');113 expect(fakeTumblrClient.link.calls[2].args[1]).toEqual({ tags: 'link', description: 'http://a.com/link x', url: 'http://a.com/link' });114 });115 it("should post a chat on demand", function() {116 var fakeTumblrClient = jasmine.createSpyObj('fakeTumblrClient', ['chat']);117 var trigger = TumblrTrigger.create("tumblrTrigger", fakeBot, { blogName: 'blogname', autoPost: true, autoPostContext: true, tumblr: fakeTumblrClient } );118 var conversation = '9:30 PM - User 1: first message\1199:30 PM - User 2: second message';120 expect(trigger.onChatMessage('roomId', 'userId', '!postchat ' + conversation, false, false)).toEqual(true);121 expect(fakeTumblrClient.chat.calls.length).toEqual(1);122 expect(fakeTumblrClient.chat.calls[0].args[0]).toEqual('blogname');123 expect(fakeTumblrClient.chat.calls[0].args[1]).toEqual({ conversation: conversation, tags: 'chat' });124 });125 it("should post audio on demand", function() {126 var fakeTumblrClient = jasmine.createSpyObj('fakeTumblrClient', ['audio']);127 var trigger = TumblrTrigger.create("tumblrTrigger", fakeBot, { blogName: 'blogname', autoPost: true, autoPostContext: true, tumblr: fakeTumblrClient } );128 expect(trigger.onChatMessage('roomId', 'userId', '!postaudio https://soundcloud.com/dumbshitthatjakazidmade/get-clucky', false, false)).toEqual(true);129 expect(fakeTumblrClient.audio.calls.length).toEqual(1);130 expect(fakeTumblrClient.audio.calls[0].args[0]).toEqual('blogname');131 expect(fakeTumblrClient.audio.calls[0].args[1]).toEqual({ tags: 'audio', external_url: 'https://soundcloud.com/dumbshitthatjakazidmade/get-clucky' });132 expect(trigger.onChatMessage('roomId', 'userId', '!postaudio https://soundcloud.com/dumbshitthatjakazidmade/get-clucky?test=param#anchor', false, false)).toEqual(true);133 expect(fakeTumblrClient.audio.calls.length).toEqual(2);134 expect(fakeTumblrClient.audio.calls[1].args[0]).toEqual('blogname');135 expect(fakeTumblrClient.audio.calls[1].args[1]).toEqual({ tags: 'audio', external_url: 'https://soundcloud.com/dumbshitthatjakazidmade/get-clucky?test=param#anchor' });136 expect(trigger.onChatMessage('roomId', 'userId', '!postaudio https://soundcloud.com cluck', false, false)).toEqual(true);137 expect(fakeTumblrClient.audio.calls.length).toEqual(3);138 expect(fakeTumblrClient.audio.calls[2].args[0]).toEqual('blogname');139 expect(fakeTumblrClient.audio.calls[2].args[1]).toEqual({ tags: 'audio', caption: 'https://soundcloud.com cluck', external_url: 'https://soundcloud.com' });140 });141 it("should post video on demand", function() {142 var fakeTumblrClient = jasmine.createSpyObj('fakeTumblrClient', ['video']);143 var trigger = TumblrTrigger.create("tumblrTrigger", fakeBot, { blogName: 'blogname', autoPost: true, autoPostContext: true, tumblr: fakeTumblrClient } );144 expect(trigger.onChatMessage('roomId', 'userId', '!postvideo http://www.youtube.com/watch?v=7E0ot9iJm_k', false, false)).toEqual(true);145 expect(fakeTumblrClient.video.calls.length).toEqual(1);146 expect(fakeTumblrClient.video.calls[0].args[0]).toEqual('blogname');147 expect(fakeTumblrClient.video.calls[0].args[1]).toEqual({ tags: 'video', embed: 'http://www.youtube.com/watch?v=7E0ot9iJm_k' });148 expect(trigger.onChatMessage('roomId', 'userId', '!postvideo http://www.youtube.com/watch?v=7E0ot9iJm_k?test=param#anchor', false, false)).toEqual(true);149 expect(fakeTumblrClient.video.calls.length).toEqual(2);150 expect(fakeTumblrClient.video.calls[1].args[0]).toEqual('blogname');151 expect(fakeTumblrClient.video.calls[1].args[1]).toEqual({ tags: 'video', embed: 'http://www.youtube.com/watch?v=7E0ot9iJm_k?test=param#anchor' });152 expect(trigger.onChatMessage('roomId', 'userId', '!postvideo http://www.youtube.com pak', false, false)).toEqual(true);153 expect(fakeTumblrClient.video.calls.length).toEqual(3);154 expect(fakeTumblrClient.video.calls[2].args[0]).toEqual('blogname');155 expect(fakeTumblrClient.video.calls[2].args[1]).toEqual({ tags: 'video', caption: 'http://www.youtube.com pak', embed: 'http://www.youtube.com' });156 });157 it("should not post malformed links automatically", function() {158 var fakeTumblrClient = jasmine.createSpyObj('fakeTumblrClient', ['link', 'photo', 'video', 'audio']);159 var trigger = TumblrTrigger.create("tumblrTrigger", fakeBot, { blogName: 'blogname', autoPost: true, autoPostContext: true, tumblr: fakeTumblrClient } );160 expect(trigger.onChatMessage('roomId', 'userId', 'mailto link mailto://email@host.com', false, false)).toEqual(false);161 expect(trigger.onChatMessage('roomId', 'userId', 'invalid schema link htp://www.google.com', false, false)).toEqual(false);162 expect(trigger.onChatMessage('roomId', 'userId', 'bad link http://', false, false)).toEqual(false);163 expect(trigger.onChatMessage('roomId', 'userId', 'bad link 2 http://!', false, false)).toEqual(false);164 expect(trigger.onChatMessage('roomId', 'userId', 'bad link 3 http://?', false, false)).toEqual(false);165 expect(fakeTumblrClient.link).not.toHaveBeenCalled();166 expect(fakeTumblrClient.photo).not.toHaveBeenCalled();167 expect(fakeTumblrClient.video).not.toHaveBeenCalled();168 expect(fakeTumblrClient.audio).not.toHaveBeenCalled();169 });170 it("should post generic links as links automatically", function() {171 var fakeTumblrClient = jasmine.createSpyObj('fakeTumblrClient', ['link', 'photo', 'video', 'audio']);172 var trigger = TumblrTrigger.create("tumblrTrigger", fakeBot, { blogName: 'blogname', autoPost: true, autoPostContext: true, tumblr: fakeTumblrClient } );173 // Automatic posts should always return false174 expect(trigger.onChatMessage('roomId', 'userId', 'http://www.google.com', false, false)).toEqual(false);175 expect(fakeTumblrClient.link.calls.length).toEqual(1);176 expect(fakeTumblrClient.link.calls[0].args[0]).toEqual('blogname');177 expect(fakeTumblrClient.link.calls[0].args[1]).toEqual({ tags: 'link', url: 'http://www.google.com' });178 expect(trigger.onChatMessage('roomId', 'userId', 'http://www.google.com with context', false, false)).toEqual(false);179 expect(fakeTumblrClient.link.calls.length).toEqual(2);180 expect(fakeTumblrClient.link.calls[1].args[0]).toEqual('blogname');181 expect(fakeTumblrClient.link.calls[1].args[1]).toEqual({ tags: 'link', url: 'http://www.google.com', description: 'http://www.google.com with context' });182 expect(trigger.onChatMessage('roomId', 'userId', 'multiple links http://x.com http://y.com', false, false)).toEqual(false);183 expect(fakeTumblrClient.link.calls.length).toEqual(3);184 expect(fakeTumblrClient.link.calls[2].args[0]).toEqual('blogname');185 expect(fakeTumblrClient.link.calls[2].args[1]).toEqual({ tags: 'link', url: 'http://x.com', description: 'multiple links http://x.com http://y.com' });186 expect(trigger.onChatMessage('roomId', 'userId', 'http://www.google.com/page', false, false)).toEqual(false);187 expect(fakeTumblrClient.link.calls.length).toEqual(4);188 expect(fakeTumblrClient.link.calls[3].args[0]).toEqual('blogname');189 expect(fakeTumblrClient.link.calls[3].args[1]).toEqual({ tags: 'link', url: 'http://www.google.com/page' });190 expect(trigger.onChatMessage('roomId', 'userId', 'http://www.google.com/page?q1=x', false, false)).toEqual(false);191 expect(fakeTumblrClient.link.calls.length).toEqual(5);192 expect(fakeTumblrClient.link.calls[4].args[0]).toEqual('blogname');193 expect(fakeTumblrClient.link.calls[4].args[1]).toEqual({ tags: 'link', url: 'http://www.google.com/page?q1=x' });194 expect(trigger.onChatMessage('roomId', 'userId', 'http://www.google.com/page?q1=x&q2=y#anchor', false, false)).toEqual(false);195 expect(fakeTumblrClient.link.calls.length).toEqual(6);196 expect(fakeTumblrClient.link.calls[5].args[0]).toEqual('blogname');197 expect(fakeTumblrClient.link.calls[5].args[1]).toEqual({ tags: 'link', url: 'http://www.google.com/page?q1=x&q2=y#anchor' });198 });199 it("should post image links as photos automatically", function() {200 var fakeTumblrClient = jasmine.createSpyObj('fakeTumblrClient', ['link', 'photo', 'video', 'audio']);201 var trigger = TumblrTrigger.create("tumblrTrigger", fakeBot, { blogName: 'blogname', autoPost: true, autoPostContext: true, tumblr: fakeTumblrClient } );202 expect(trigger.onChatMessage('roomId', 'userId', 'http://www.google.com/image.jpg', false, false)).toEqual(false);203 expect(fakeTumblrClient.photo.calls.length).toEqual(1);204 expect(fakeTumblrClient.photo.calls[0].args[1]).toEqual({ tags: 'photo', source: 'http://www.google.com/image.jpg', link: 'http://www.google.com/image.jpg' });205 expect(trigger.onChatMessage('roomId', 'userId', 'http://www.google.com/image.jpeg', false, false)).toEqual(false);206 expect(fakeTumblrClient.photo.calls.length).toEqual(2);207 expect(fakeTumblrClient.photo.calls[1].args[1]).toEqual({ tags: 'photo', source: 'http://www.google.com/image.jpeg', link: 'http://www.google.com/image.jpeg' });208 expect(trigger.onChatMessage('roomId', 'userId', 'http://www.google.com/image.jpeg', false, false)).toEqual(false);209 expect(fakeTumblrClient.photo.calls.length).toEqual(3);210 expect(fakeTumblrClient.photo.calls[2].args[1]).toEqual({ tags: 'photo', source: 'http://www.google.com/image.jpeg', link: 'http://www.google.com/image.jpeg' });211 expect(trigger.onChatMessage('roomId', 'userId', 'http://www.google.com/image.png', false, false)).toEqual(false);212 expect(fakeTumblrClient.photo.calls.length).toEqual(4);213 expect(fakeTumblrClient.photo.calls[3].args[1]).toEqual({ tags: 'photo', source: 'http://www.google.com/image.png', link: 'http://www.google.com/image.png' });214 expect(trigger.onChatMessage('roomId', 'userId', 'http://www.google.com/image.bmp', false, false)).toEqual(false);215 expect(fakeTumblrClient.photo.calls.length).toEqual(5);216 expect(fakeTumblrClient.photo.calls[4].args[1]).toEqual({ tags: 'photo', source: 'http://www.google.com/image.bmp', link: 'http://www.google.com/image.bmp' });217 expect(trigger.onChatMessage('roomId', 'userId', 'http://www.google.com/image.JPg', false, false)).toEqual(false);218 expect(fakeTumblrClient.photo.calls.length).toEqual(6);219 expect(fakeTumblrClient.photo.calls[5].args[1]).toEqual({ tags: 'photo', source: 'http://www.google.com/image.JPg', link: 'http://www.google.com/image.JPg' });220 expect(trigger.onChatMessage('roomId', 'userId', 'http://www.google.com/image.jpg context', false, false)).toEqual(false);221 expect(fakeTumblrClient.photo.calls.length).toEqual(7);222 expect(fakeTumblrClient.photo.calls[6].args[1]).toEqual({ tags: 'photo', source: 'http://www.google.com/image.jpg', link: 'http://www.google.com/image.jpg', caption: 'http://www.google.com/image.jpg context' });223 expect(fakeTumblrClient.link).not.toHaveBeenCalled();224 });225 it("should post bad image links as links", function() {226 var fakeTumblrClient = jasmine.createSpyObj('fakeTumblrClient', ['link', 'photo', 'video', 'audio']);227 var trigger = TumblrTrigger.create("tumblrTrigger", fakeBot, { blogName: 'blogname', autoPost: true, autoPostContext: true, tumblr: fakeTumblrClient } );228 expect(trigger.onChatMessage('roomId', 'userId', 'http://www.google.com/image .jpg', false, false)).toEqual(false);229 expect(trigger.onChatMessage('roomId', 'userId', 'http://www.google.com/xjpg', false, false)).toEqual(false);230 expect(trigger.onChatMessage('roomId', 'userId', 'http://www.google.com/a.jpg?x=y', false, false)).toEqual(false);231 expect(trigger.onChatMessage('roomId', 'userId', 'http://www.google.com/a.jpg#a', false, false)).toEqual(false);232 expect(fakeTumblrClient.photo).not.toHaveBeenCalled();233 expect(fakeTumblrClient.link.calls.length).toEqual(4);234 });235 it("should post tumblr gifs as photos", function() {236 var fakeTumblrClient = jasmine.createSpyObj('fakeTumblrClient', ['link', 'photo', 'video', 'audio', 'text']);237 var trigger = TumblrTrigger.create("tumblrTrigger", fakeBot, { blogName: 'blogname', autoPost: true, autoPostContext: true, tumblr: fakeTumblrClient } );238 expect(trigger.onChatMessage('roomId', 'userId', 'http://25.media.tumblr.com/x/x.gif', false, false)).toEqual(false);239 expect(fakeTumblrClient.photo.calls.length).toEqual(1);240 expect(fakeTumblrClient.photo.calls[0].args[1]).toEqual({ tags: 'photo', source: 'http://25.media.tumblr.com/x/x.gif', link: 'http://25.media.tumblr.com/x/x.gif' });241 expect(trigger.onChatMessage('roomId', 'userId', 'http://25.media.tumblr.com/x/x.gif context', false, false)).toEqual(false);242 expect(fakeTumblrClient.photo.calls.length).toEqual(2);243 expect(fakeTumblrClient.photo.calls[1].args[1]).toEqual({ tags: 'photo', source: 'http://25.media.tumblr.com/x/x.gif', link: 'http://25.media.tumblr.com/x/x.gif', caption: 'http://25.media.tumblr.com/x/x.gif context' });244 expect(fakeTumblrClient.link).not.toHaveBeenCalled();245 expect(fakeTumblrClient.text).not.toHaveBeenCalled();246 });247 it("should post non-tumblr gifs as HTML text tagged as photo", function() {248 var fakeTumblrClient = jasmine.createSpyObj('fakeTumblrClient', ['link', 'photo', 'video', 'audio', 'text']);249 var trigger = TumblrTrigger.create("tumblrTrigger", fakeBot, { blogName: 'blogname', autoPost: true, autoPostContext: true, tumblr: fakeTumblrClient } );250 expect(trigger.onChatMessage('roomId', 'userId', 'http://i.imgur.com/x.gif', false, false)).toEqual(false);251 252 expect(fakeTumblrClient.text.calls.length).toEqual(1);253 expect(fakeTumblrClient.text.calls[0].args[1]).toEqual({ tags: 'photo', body: '<img src="http://i.imgur.com/x.gif">' });254 expect(trigger.onChatMessage('roomId', 'userId', 'http://i.imgur.com/x.gif context', false, false)).toEqual(false);255 expect(fakeTumblrClient.text.calls.length).toEqual(2);256 expect(fakeTumblrClient.text.calls[1].args[1]).toEqual({ tags: 'photo', body: '<img src="http://i.imgur.com/x.gif"><br>http://i.imgur.com/x.gif context' });257 expect(fakeTumblrClient.link).not.toHaveBeenCalled();258 expect(fakeTumblrClient.photo).not.toHaveBeenCalled();259 });260 it("should post video links as video automatically", function() {261 var fakeTumblrClient = jasmine.createSpyObj('fakeTumblrClient', ['link', 'photo', 'video', 'audio']);262 var trigger = TumblrTrigger.create("tumblrTrigger", fakeBot, { blogName: 'blogname', autoPost: true, autoPostContext: true, tumblr: fakeTumblrClient } );263 expect(trigger.onChatMessage('roomId', 'userId', 'http://www.youtube.com/watch?v=xx', false, false)).toEqual(false);264 expect(fakeTumblrClient.video.calls.length).toEqual(1);265 expect(fakeTumblrClient.video.calls[0].args[1]).toEqual({ tags: 'video', embed: 'http://www.youtube.com/watch?v=xx' });266 expect(trigger.onChatMessage('roomId', 'userId', 'https://www.youtube.com/watch?v=xx context', false, false)).toEqual(false);267 expect(fakeTumblrClient.video.calls.length).toEqual(2);268 expect(fakeTumblrClient.video.calls[1].args[1]).toEqual({ tags: 'video', embed: 'https://www.youtube.com/watch?v=xx', caption: 'https://www.youtube.com/watch?v=xx context' });269 expect(trigger.onChatMessage('roomId', 'userId', 'http://vimeo.com/xx', false, false)).toEqual(false);270 expect(fakeTumblrClient.video.calls.length).toEqual(3);271 expect(fakeTumblrClient.video.calls[2].args[1]).toEqual({ tags: 'video', embed: 'http://vimeo.com/xx' });272 expect(trigger.onChatMessage('roomId', 'userId', 'http://x.subdomain.vimeo.com/xx', false, false)).toEqual(false);273 expect(fakeTumblrClient.video.calls.length).toEqual(4);274 expect(fakeTumblrClient.video.calls[3].args[1]).toEqual({ tags: 'video', embed: 'http://x.subdomain.vimeo.com/xx' });275 expect(fakeTumblrClient.link).not.toHaveBeenCalled();276 });277 it("should post bad video links as links", function() {278 var fakeTumblrClient = jasmine.createSpyObj('fakeTumblrClient', ['link', 'photo', 'video', 'audio']);279 var trigger = TumblrTrigger.create("tumblrTrigger", fakeBot, { blogName: 'blogname', autoPost: true, autoPostContext: true, tumblr: fakeTumblrClient } );280 expect(trigger.onChatMessage('roomId', 'userId', 'http://www.youtube.com/', false, false)).toEqual(false);281 expect(trigger.onChatMessage('roomId', 'userId', 'http://www.youtube.com', false, false)).toEqual(false);282 expect(trigger.onChatMessage('roomId', 'userId', 'http://theyoutube.com/xx', false, false)).toEqual(false);283 expect(trigger.onChatMessage('roomId', 'userId', 'http://youtubed.com/xx', false, false)).toEqual(false);284 expect(fakeTumblrClient.video).not.toHaveBeenCalled();285 expect(fakeTumblrClient.link.calls.length).toEqual(4);286 });287 it("should post audio links as audio automatically", function() {288 var fakeTumblrClient = jasmine.createSpyObj('fakeTumblrClient', ['link', 'photo', 'video', 'audio']);289 var trigger = TumblrTrigger.create("tumblrTrigger", fakeBot, { blogName: 'blogname', autoPost: true, autoPostContext: true, tumblr: fakeTumblrClient } );290 expect(trigger.onChatMessage('roomId', 'userId', 'http://google.com/audio.mp3', false, false)).toEqual(false);291 expect(fakeTumblrClient.audio.calls.length).toEqual(1);292 expect(fakeTumblrClient.audio.calls[0].args[1]).toEqual({ tags: 'audio', external_url: 'http://google.com/audio.mp3' });293 expect(trigger.onChatMessage('roomId', 'userId', 'http://google.com/aud(iox-x.mp3', false, false)).toEqual(false);294 expect(fakeTumblrClient.audio.calls.length).toEqual(2);295 //expect(fakeTumblrClient.audio.calls[1].args[1]).toEqual({ tags: 'audio', external_url: 'http://google.com/audio(x-.mp3' });296 expect(trigger.onChatMessage('roomId', 'userId', 'http://google.com/audio.wav context', false, false)).toEqual(false);297 expect(fakeTumblrClient.audio.calls.length).toEqual(3);298 expect(fakeTumblrClient.audio.calls[2].args[1]).toEqual({ tags: 'audio', external_url: 'http://google.com/audio.wav', caption: 'http://google.com/audio.wav context' });299 expect(trigger.onChatMessage('roomId', 'userId', 'http://soundcloud.com/xx', false, false)).toEqual(false);300 expect(fakeTumblrClient.audio.calls.length).toEqual(4);301 expect(fakeTumblrClient.audio.calls[3].args[1]).toEqual({ tags: 'audio', external_url: 'http://soundcloud.com/xx' });302 expect(trigger.onChatMessage('roomId', 'userId', 'https://x.subdomain.soundcloud.com/xx', false, false)).toEqual(false);303 expect(fakeTumblrClient.audio.calls.length).toEqual(5);304 expect(fakeTumblrClient.audio.calls[4].args[1]).toEqual({ tags: 'audio', external_url: 'https://x.subdomain.soundcloud.com/xx' });305 expect(fakeTumblrClient.link).not.toHaveBeenCalled();306 });307 it("should not post context if the autoPostContext option is false", function() {308 var fakeTumblrClient = jasmine.createSpyObj('fakeTumblrClient', ['link', 'photo', 'video', 'audio']);309 var trigger = TumblrTrigger.create("tumblrTrigger", fakeBot, { blogName: 'blogname', autoPost: true, autoPostContext: false, tumblr: fakeTumblrClient } );310 expect(trigger.onChatMessage('roomId', 'userId', 'context http://www.google.com context', false, false)).toEqual(false);311 expect(fakeTumblrClient.link.calls.length).toEqual(1);312 expect(fakeTumblrClient.link.calls[0].args[0]).toEqual('blogname');313 expect(fakeTumblrClient.link.calls[0].args[1]).toEqual({ tags: 'link', url: 'http://www.google.com' });314 });315 it("should not post context if the autoPostContext option is missing", function() {316 var fakeTumblrClient = jasmine.createSpyObj('fakeTumblrClient', ['link', 'photo', 'video', 'audio']);317 var trigger = TumblrTrigger.create("tumblrTrigger", fakeBot, { blogName: 'blogname', autoPost: true, tumblr: fakeTumblrClient } );318 expect(trigger.onChatMessage('roomId', 'userId', 'context http://www.google.com context', false, false)).toEqual(false);319 expect(fakeTumblrClient.link.calls.length).toEqual(1);320 expect(fakeTumblrClient.link.calls[0].args[0]).toEqual('blogname');321 expect(fakeTumblrClient.link.calls[0].args[1]).toEqual({ tags: 'link', url: 'http://www.google.com' });322 });323 it("should post each link separately if the autoPostContext option is false", function() {324 var fakeTumblrClient = jasmine.createSpyObj('fakeTumblrClient', ['link', 'photo', 'video', 'audio']);325 var trigger = TumblrTrigger.create("tumblrTrigger", fakeBot, { blogName: 'blogname', autoPost: true, autoPostContext: false, tumblr: fakeTumblrClient } );326 expect(trigger.onChatMessage('roomId', 'userId', 'context http://www.google.com/1 context http://www.google.com/2', false, false)).toEqual(false);327 expect(fakeTumblrClient.link.calls.length).toEqual(2);328 expect(fakeTumblrClient.link.calls[0].args[0]).toEqual('blogname');329 expect(fakeTumblrClient.link.calls[0].args[1]).toEqual({ tags: 'link', url: 'http://www.google.com/1' });330 expect(fakeTumblrClient.link.calls[1].args[0]).toEqual('blogname');331 expect(fakeTumblrClient.link.calls[1].args[1]).toEqual({ tags: 'link', url: 'http://www.google.com/2' });332 expect(trigger.onChatMessage('roomId', 'userId', 'http://google.com/audio.mp3 http://www.youtube.com/watch?v=xx http://www.google.com/image.jpg', false, false)).toEqual(false);333 expect(fakeTumblrClient.link.calls.length).toEqual(2);334 expect(fakeTumblrClient.audio.calls.length).toEqual(1);335 expect(fakeTumblrClient.audio.calls[0].args[0]).toEqual('blogname');336 expect(fakeTumblrClient.audio.calls[0].args[1]).toEqual({ tags: 'audio', external_url: 'http://google.com/audio.mp3' });337 expect(fakeTumblrClient.video.calls.length).toEqual(1);338 expect(fakeTumblrClient.video.calls[0].args[0]).toEqual('blogname');339 expect(fakeTumblrClient.video.calls[0].args[1]).toEqual({ tags: 'video', embed: 'http://www.youtube.com/watch?v=xx' });340 expect(fakeTumblrClient.photo.calls.length).toEqual(1);341 expect(fakeTumblrClient.photo.calls[0].args[0]).toEqual('blogname');342 expect(fakeTumblrClient.photo.calls[0].args[1]).toEqual({ tags: 'photo', source: 'http://www.google.com/image.jpg', link: 'http://www.google.com/image.jpg' });343 });344 it("should not auto-post links if the autoPost option is false", function() {345 var fakeTumblrClient = jasmine.createSpyObj('fakeTumblrClient', ['link', 'photo', 'video', 'audio']);346 var trigger = TumblrTrigger.create("tumblrTrigger", fakeBot, { blogName: 'blogname', autoPost: false, tumblr: fakeTumblrClient } );347 expect(trigger.onChatMessage('roomId', 'userId', 'http://www.google.com/1', false, false)).toEqual(false);348 expect(fakeTumblrClient.link).not.toHaveBeenCalled();349 expect(trigger.onChatMessage('roomId', 'userId', '!postlink http://www.google.com/1', false, false)).toEqual(true);350 expect(fakeTumblrClient.link.calls.length).toEqual(1);351 expect(fakeTumblrClient.link.calls[0].args[0]).toEqual('blogname');352 expect(fakeTumblrClient.link.calls[0].args[1]).toEqual({ tags: 'link', url: 'http://www.google.com/1' });353 });...

Full Screen

Full Screen

editor.js

Source:editor.js Github

copy

Full Screen

1import expect, { createSpy } from "expect"2import rewiremock from "rewiremock"3import Enzyme, { shallow } from "enzyme"4import Adapter from "enzyme-adapter-react-15"5import React from "react"6import FakeAce, { Session } from "test/unit/mocks/ace.js"7import { fromJS } from "immutable"8const pause = (ms) => new Promise((res) => setTimeout(res, ms))9const EVENTUALLY = 900 // ms10/**11* We're mocking out the editor,12* so uses of the phrase "should see this in editor",13* will match to the following Ace methods:14*15* - "what user see's in editor" => fakeAce.userSees()16* - "user types something (end of document)" => fakeAce.userTypes("hi")17* - "Ctrl-Z" => fakeAce.userUndo()18* - "Ctrl-Shift-Z" => fakeAce.userRedo()19**/20describe("editor", function() {21 before(function () {22 // Enzyme.configure({ adapter: new Adapter()})23 Enzyme.configure({ adapter: new Adapter()})24 // Whole bunch of mocks!25 rewiremock.enable()26 rewiremock("brace/mode/yaml").with({})27 rewiremock("brace/theme/tomorrow_night_eighties").with({})28 rewiremock("brace/ext/language_tools").with({})29 rewiremock("brace/ext/searchbox").with({})30 rewiremock("./brace-snippets-yaml").with({})31 rewiremock("./editor.less").with({})32 })33 after(function() {34 rewiremock.disable()35 })36 beforeEach(function() {37 delete require.cache[require.resolve("react-ace")]38 })39 describe("fake ace", function() {40 it("should be an event emitter", () => {41 // Given42 const fakeAce = new FakeAce()43 const spy = createSpy()44 fakeAce.on("foo", spy)45 // When46 fakeAce.emit("foo", "bar")47 // Then48 expect(spy.calls.length).toEqual(1)49 expect(spy.calls[0].arguments[0]).toEqual("bar")50 })51 it("should return `this`, when calling .edit", function() {52 // Given53 const fakeAce = new FakeAce()54 // When55 const res = fakeAce.edit()56 // Then57 expect(res).toBe(fakeAce)58 })59 it("should keep track of setValue", function() {60 // Given61 const fakeAce = new FakeAce()62 // When63 fakeAce.setValue("foo")64 // Then65 const res = fakeAce.getValue()66 expect(res).toEqual("foo")67 })68 it("should spy on setValue", function() {69 // Given70 const fakeAce = new FakeAce()71 // When72 fakeAce.setValue("foo")73 // Then74 expect(fakeAce.setValue.calls.length).toEqual(1)75 expect(fakeAce.setValue.calls[0].arguments[0]).toEqual("foo")76 })77 it("should return a single session, with getSession", function() {78 // Given79 const fakeAce = new FakeAce()80 // When81 const res = fakeAce.getSession()82 // Then83 expect(res).toBeA(Session)84 })85 it("should add options via setOptions", function() {86 // Given87 const fakeAce = new FakeAce()88 // When89 fakeAce.setOptions({one: "uno"})90 // Then91 const res = fakeAce.getOption("one")92 expect(res).toEqual("uno")93 })94 describe("userUndo/Redo", function() {95 it("should revert to last input", function() {96 // Given97 const fakeAce = new FakeAce()98 fakeAce.userTypes("one")99 // When100 fakeAce.userTypes("two")101 // Then102 fakeAce.userUndo()103 expect(fakeAce.userSees()).toEqual("one")104 })105 it("should revert to empty document, no changes were made", function() {106 // Given107 const fakeAce = new FakeAce()108 // When109 fakeAce.userUndo()110 // Then111 expect(fakeAce.userSees()).toEqual("")112 })113 it("should revert to empty document, after arbitrary undos", function() {114 // Given115 const fakeAce = new FakeAce()116 // When117 fakeAce.userUndo()118 fakeAce.userUndo()119 fakeAce.userUndo()120 fakeAce.userUndo()121 // Then122 expect(fakeAce.userSees()).toEqual("")123 })124 it("should not extend redos after last change", function() {125 // Given126 const fakeAce = new FakeAce()127 fakeAce.userTypes("x")128 // When129 fakeAce.userRedo()130 fakeAce.userRedo()131 fakeAce.userRedo()132 // Then133 expect(fakeAce.userSees()).toEqual("x")134 })135 it("should allow redo after single undo", function() {136 // Given137 const fakeAce = new FakeAce()138 fakeAce.userTypes("x")139 fakeAce.userTypes("x")140 fakeAce.userUndo()141 // When142 fakeAce.userRedo()143 // Then144 expect(fakeAce.userSees()).toEqual("xx")145 })146 it("should create new thread of undo stack, after new change", function() {147 // Given148 const fakeAce = new FakeAce()149 fakeAce.userTypes("1")150 fakeAce.userTypes("2")151 fakeAce.userTypes("3")152 fakeAce.userTypes("4")153 fakeAce.userUndo() // 123154 fakeAce.userUndo() // 12155 fakeAce.userTypes("5") // 125156 // When157 fakeAce.userRedo() // 125, don't extend beyond158 // Then159 expect(fakeAce.userSees()).toEqual("125")160 })161 })162 describe("fake session", function() {163 it("should keep add state for markers", function() {164 // Given165 const fakeAce = new FakeAce()166 const fakeSession = fakeAce.getSession()167 // When168 fakeSession.addMarker({one: 1})169 // Then170 const res = fakeSession.getMarkers()171 expect(res).toBeAn("array")172 expect(res.length).toEqual(1)173 expect(res[0]).toEqual({id: 0, one: 1})174 })175 it("should keep remove state for markers", function() {176 // Given177 const fakeAce = new FakeAce()178 const fakeSession = fakeAce.getSession()179 fakeSession.addMarker({one: 1})180 // When181 fakeSession.removeMarker(0)182 // Then183 const res = fakeSession.getMarkers()184 expect(res).toBeAn("array")185 expect(res.length).toEqual(0)186 })187 it("should spy on addMarker", function() {188 // Given189 const fakeAce = new FakeAce()190 const fakeSession = fakeAce.getSession()191 // When192 fakeSession.addMarker({one: 1})193 // Then194 expect(fakeSession.addMarker.calls.length).toEqual(1)195 })196 it("should spy on setMode", function() {197 // Given198 const fakeAce = new FakeAce()199 const fakeSession = fakeAce.getSession()200 // When201 fakeSession.setMode()202 // Then203 expect(fakeSession.setMode.calls.length).toEqual(1)204 })205 it("should have a .selection which includes toJSON, fromJSON", function() {206 // Given207 const fakeAce = new FakeAce()208 // When209 const fakeSession = fakeAce.getSession()210 // Then211 expect(fakeSession.selection).toIncludeKey("toJSON")212 expect(fakeSession.selection).toIncludeKey("fromJSON")213 })214 describe("userTypes", function() {215 it("should emit 'change'", function() {216 // Given217 const fakeAce = new FakeAce()218 const spy = createSpy()219 fakeAce.on("change", spy)220 // When221 fakeAce.userTypes("hello")222 // Then223 expect(spy.calls.length).toBeGreaterThan(1)224 })225 it("should change the value", function() {226 // Given227 const fakeAce = new FakeAce()228 // When229 fakeAce.userTypes("hello")230 // Then231 expect(fakeAce.getValue()).toEqual("hello")232 })233 })234 describe("userSees", function() {235 it("should match userTypes", function() {236 // Given237 const fakeAce = new FakeAce()238 // When239 fakeAce.userTypes("hi")240 // Then241 const res = fakeAce.userSees()242 expect(res).toEqual("hi")243 })244 it("should match setValue", function() {245 // Given246 const fakeAce = new FakeAce()247 // When248 fakeAce.setValue("hello")249 // Then250 const res = fakeAce.userSees()251 expect(res).toEqual("hello")252 })253 })254 })255 describe("renderer", function() {256 it("should have a stub for setShowGutter", function() {257 // Given258 const fakeAce = new FakeAce()259 // When260 fakeAce.renderer.setShowGutter("foo")261 // Then262 expect(fakeAce.renderer.setShowGutter.calls.length).toEqual(1)263 expect(fakeAce.renderer.setShowGutter.calls[0].arguments[0]).toEqual("foo")264 })265 })266 })267 describe("editor component", function() {268 it("should EVENTUALLY call onChange when user enters input", (done) => {269 // Given270 const fakeAce = new FakeAce()271 rewiremock("brace").with(fakeAce)272 const makeEditor = require("plugins/editor/components/editor.jsx").default273 const Editor = makeEditor({})274 const spy = createSpy()275 const wrapper = shallow(276 <Editor onChange={spy} />277 )278 wrapper279 .find("ReactAce").shallow()280 // When281 // Simulate user input282 fakeAce.userTypes("hello")283 // Then284 setTimeout(() => {285 expect(spy.calls.length).toEqual(1)286 expect(spy.calls[0].arguments[0]).toEqual("hello")287 done()288 }, EVENTUALLY)289 })290 it("should EVENTUALLY put the contents of `value` prop into editor, without regard to `origin` property", (done) => {291 // Given292 const fakeAce = new FakeAce()293 rewiremock("brace").with(fakeAce)294 const makeEditor = require("plugins/editor/components/editor.jsx").default295 const Editor = makeEditor({})296 // When297 const wrapper = shallow(298 <Editor value={"original value"} />299 )300 wrapper.find("ReactAce").shallow()301 // Then302 setTimeout(() => {303 expect(fakeAce.userSees()).toEqual("original value")304 done()305 }, EVENTUALLY)306 })307 it("should EVENTUALLY put the contents of `value` prop into editor, with `foo` origin property ", (done) => {308 // Given309 const fakeAce = new FakeAce()310 rewiremock("brace").with(fakeAce)311 const makeEditor = require("plugins/editor/components/editor.jsx").default312 const Editor = makeEditor({})313 // When314 const wrapper = shallow(315 <Editor value={"original value"} origin="foo" />316 )317 wrapper.find("ReactAce").shallow()318 // Then319 setTimeout(() => {320 expect(fakeAce.userSees()).toEqual("original value")321 done()322 }, EVENTUALLY)323 })324 it("should NEVER update ace if the yaml originated in editor", async () => {325 // Given326 const fakeAce = new FakeAce()327 rewiremock("brace").with(fakeAce)328 const makeEditor = require("plugins/editor/components/editor.jsx").default329 const Editor = makeEditor({})330 // When331 const wrapper = shallow(332 <Editor value="original value" />333 )334 wrapper.find("ReactAce").shallow()335 wrapper.setProps({value: "new value", origin: "editor"})336 // Then337 await pause(EVENTUALLY)338 expect(fakeAce.userSees()).toEqual("original value")339 })340 // SKIPPED: Does this have any value at this level? And not editor-container?341 it.skip("SKIP: should EVENTUALLY call onChange ONCE if the user types/pauses/types", async function() {342 this.timeout(10000)343 // Given344 const fakeAce = new FakeAce()345 rewiremock("brace").with(fakeAce)346 const makeEditor = require("plugins/editor/components/editor.jsx").default347 const Editor = makeEditor({})348 const spy = createSpy()349 const wrapper = shallow(350 <Editor value="original value" onChange={spy}/>351 )352 wrapper.find("ReactAce").shallow()353 // When354 fakeAce.userTypes(" one")355 await pause(EVENTUALLY / 2)356 fakeAce.userTypes("two")357 await pause(EVENTUALLY / 2)358 fakeAce.userTypes("three")359 await pause(EVENTUALLY / 2)360 await pause(EVENTUALLY * 2)361 expect(fakeAce.userSees()).toEqual("original value onetwothree")362 expect(spy.calls.length).toEqual(1)363 })364 it("should EVENTUALLY call onChange when ctrl-z", async function() {365 this.timeout(10000)366 // Given367 const fakeAce = new FakeAce()368 rewiremock("brace").with(fakeAce)369 const makeEditor = require("plugins/editor/components/editor.jsx").default370 const Editor = makeEditor({})371 const spy = createSpy()372 const wrapper = shallow(373 <Editor value="original value" onChange={spy}/>374 )375 wrapper.find("ReactAce").shallow()376 fakeAce.userTypes("one")377 // When378 fakeAce.userUndo()379 await pause(EVENTUALLY)380 expect(fakeAce.userSees()).toEqual("original value")381 expect(spy.calls.length).toEqual(1)382 })383 describe("markers", function() {384 it("should place markers into editor", async function() {385 // Given386 const fakeAce = new FakeAce()387 const spy = createSpy()388 rewiremock("brace").with(fakeAce)389 rewiremock("../editor-helpers/marker-placer").with({placeMarkerDecorations: spy})390 const makeEditor = require("plugins/editor/components/editor.jsx").default391 const Editor = makeEditor({})392 const dummy = fromJS({one: 1})393 const wrapper = shallow(394 <Editor markers={dummy} />395 )396 // When397 wrapper.find("ReactAce").shallow()398 await pause(EVENTUALLY)399 // Then400 expect(spy.calls.length).toEqual(1)401 expect(spy.calls[0].arguments[0]).toInclude({markers: {one: 1}})402 })403 it("should place markers after yaml", async function() {404 // Given405 const order = []406 const fakeAce = new FakeAce()407 fakeAce.setValue.andCall(() => order.push("setValue"))408 const spy = createSpy().andCall(() => order.push("placeMarkers"))409 rewiremock("brace").with(fakeAce)410 rewiremock("../editor-helpers/marker-placer").with({placeMarkerDecorations: spy})411 const makeEditor = require("plugins/editor/components/editor.jsx").default412 const Editor = makeEditor({})413 const wrapper = shallow(414 <Editor value="original value" markers={{}} />415 )416 // When417 wrapper.find("ReactAce").shallow()418 await pause(EVENTUALLY)419 // Then420 expect(order).toEqual(["setValue", "placeMarkers"])421 })422 it.skip("should Test for markers being disabled/enabled during a yaml update", async function() {423 // Given424 const order = []425 const fakeAce = new FakeAce()426 fakeAce.setValue.andCall(() => order.push("setValue"))427 const spy = createSpy().andCall(() => {428 order.push("placeMarkers")429 return () => order.push("removeMarkers")430 })431 rewiremock("brace").with(fakeAce)432 rewiremock("../editor-helpers/marker-placer").with({placeMarkerDecorations: spy})433 const makeEditor = require("plugins/editor/components/editor.jsx").default434 const Editor = makeEditor({})435 const wrapper = shallow(436 <Editor value="original value" markers={{}} />437 )438 wrapper.find("ReactAce").shallow()439 // When440 wrapper.setProps({value: "new value", origin: "bob"})441 await pause(EVENTUALLY)442 // Then443 expect(order).toEqual(["setValue", "placeMarkers", "removeMarkers", "setValue", "placeMarkers"])444 })445 it.skip("should Test for markers being disabled/enabled during ctrl-z", async function() {446 // Given447 const order = []448 const fakeAce = new FakeAce()449 fakeAce.setValue.andCall(() => order.push("setValue"))450 const spy = createSpy().andCall(() => {451 order.push("placeMarkers")452 return () => order.push("removeMarkers")453 })454 rewiremock("brace").with(fakeAce)455 rewiremock("../editor-helpers/marker-placer").with({placeMarkerDecorations: spy})456 const makeEditor = require("plugins/editor/components/editor.jsx").default457 const Editor = makeEditor({})458 const wrapper = shallow(459 <Editor value="original value" markers={{}} />460 )461 wrapper.find("ReactAce").shallow()462 // When463 fakeAce.userUndo()464 await pause(EVENTUALLY)465 // Then466 expect(order).toEqual(["setValue", "placeMarkers", "removeMarkers", "setValue", "placeMarkers"])467 })468 })469 })...

Full Screen

Full Screen

chatBotSpec.js

Source:chatBotSpec.js Github

copy

Full Screen

1var _ = require('underscore');2var steam = require('steam');3var ChatBot = require('../lib/chatBot.js').ChatBot;4describe("ChatBot", function() {5 var bot;6 var fakeClient;7 var fakeTriggerFactory;8 beforeEach(function() {9 fakeClient = jasmine.createSpyObj('fakeClient', ['on', 'logOn', 'setPersonaState', 'joinChat']);10 fakeTriggerFactory = jasmine.createSpyObj('fakeTriggerFactory', ['createTrigger']);11 fakeTriggerFactory.createTrigger.andCallFake(function(type, name, bot, options) {12 if (type == "invalidType") {13 return null;14 }15 var fakeTrigger = jasmine.createSpyObj('fakeTrigger', ['getOptions', 'onChatInvite', 'onFriendRequest', 'onFriendMessage', 'onChatMessage']);16 fakeTrigger.getOptions.andCallFake(function() {17 return options;18 });19 fakeTrigger.name = name;20 fakeTrigger.type = type;21 return fakeTrigger;22 });23 });24 afterEach(function() {25 if (bot) {26 clearInterval(bot.babysitInterval);27 }28 });29 describe("ChatBot connections", function() {30 it("should connect when created if autoConnect option is on", function() {31 bot = new ChatBot("username", "password", { client: fakeClient, autoConnect: true, autoReconnect: true });32 expect(fakeClient.logOn).toHaveBeenCalled();33 });34 it("should reconnect when disconnected if autoReconnect option is on", function() {35 runs(function() {36 bot = new ChatBot("username", "password", { client: fakeClient, babysitTimer: 100, autoConnect: true, autoReconnect: true });37 expect(fakeClient.logOn.calls.length).toEqual(1);38 bot.connected = false;39 });40 waits(110);41 runs(function() {42 expect(fakeClient.logOn.calls.length).toEqual(2);43 });44 });45 it("should not automatically connect if autoConnect option is off", function() {46 bot = new ChatBot("username", "password", { client: fakeClient, autoConnect: false, autoReconnect: false });47 expect(fakeClient.logOn).not.toHaveBeenCalled();48 bot.connect();49 expect(fakeClient.logOn).toHaveBeenCalled();50 });51 it("should not automatically re-connect until connect is called if autoConnect option is off", function() {52 bot = new ChatBot("username", "password", { client: fakeClient, autoConnect: false, autoReconnect: true, babysitTimer: 100 });53 54 waits(110);55 runs(function() {56 expect(fakeClient.logOn).not.toHaveBeenCalled();57 bot.connect();58 expect(fakeClient.logOn).toHaveBeenCalled();59 bot.connected = false;60 });61 waits(110);62 runs(function() {63 expect(fakeClient.logOn.calls.length).toEqual(2);64 });65 });66 it("should not automatically re-connect on disconnection if autoReconnect option is off", function() {67 bot = new ChatBot("username", "password", { client: fakeClient, autoConnect: false, autoReconnect: false, babysitTimer: 100 });68 expect(fakeClient.logOn).not.toHaveBeenCalled();69 bot.connect();70 expect(fakeClient.logOn).toHaveBeenCalled();71 bot.connected = false;72 waits(110);73 runs(function() {74 expect(fakeClient.logOn.calls.length).toEqual(1);75 });76 });77 });78 describe("ChatBot muting", function() {79 it("should update its persona state when muted and unmuted", function() {80 bot = new ChatBot("username", "password", { client: fakeClient });81 bot._onLoggedOn(); // Fake logged on event82 expect(fakeClient.setPersonaState.calls.length).toEqual(1);83 expect(fakeClient.setPersonaState.calls[0].args[0]).toEqual(bot.unmutedState);84 bot.mute();85 expect(fakeClient.setPersonaState.calls.length).toEqual(2);86 expect(fakeClient.setPersonaState.calls[1].args[0]).toEqual(bot.mutedState);87 bot.unmute();88 expect(fakeClient.setPersonaState.calls.length).toEqual(3);89 expect(fakeClient.setPersonaState.calls[2].args[0]).toEqual(bot.unmutedState);90 });91 it("should log in with the muted persona state after reconnection when muted", function() {92 runs(function() {93 bot = new ChatBot("username", "password", { client: fakeClient, babysitTimer: 100 });94 bot._onLoggedOn(); // Fake logged on event95 bot.mute();96 bot.connected = false;97 setTimeout(function() {98 bot._onLoggedOn(); // Fake logged on event99 }, 110);100 });101 waits(200);102 runs(function() {103 expect(fakeClient.setPersonaState.calls.length).toEqual(3); // first log-on, mute, reconnect104 expect(fakeClient.setPersonaState.calls[2].args[0]).toEqual(bot.mutedState);105 });106 });107 });108 describe("ChatBot trigger management", function() {109 it("should return true when a valid trigger is added", function() {110 bot = new ChatBot("username", "password", { client: fakeClient, triggerFactory: fakeTriggerFactory });111 expect(bot.addTrigger("trigger1", "type")).not.toBeNull();112 });113 it("should return false when a trigger can't be created", function() {114 bot = new ChatBot("username", "password", { client: fakeClient, triggerFactory: fakeTriggerFactory });115 expect(bot.addTrigger("trigger1", "invalidType")).toBeNull();116 });117 it("should replace a trigger when one is added with the same name", function() {118 bot = new ChatBot("username", "password", { client: fakeClient, triggerFactory: fakeTriggerFactory });119 expect(bot.addTrigger("trigger1", "type1")).not.toBeNull();120 expect(bot.addTrigger("trigger1", "type2")).not.toBeNull();121 expect(_.size(bot.triggers)).toEqual(1);122 });123 it("should return true when a trigger is removed", function() {124 bot = new ChatBot("username", "password", { client: fakeClient, triggerFactory: fakeTriggerFactory });125 expect(bot.addTrigger("trigger1", "type")).not.toBeNull();126 expect(bot.removeTrigger("trigger1")).toEqual(true);127 expect(_.size(bot.triggers)).toEqual(0);128 });129 it("should return false when remove is called for a non-existent trigger", function() {130 bot = new ChatBot("username", "password", { client: fakeClient, triggerFactory: fakeTriggerFactory });131 expect(bot.removeTrigger("trigger")).toEqual(false);132 });133 it("should allow multiple triggers to be added in the same call", function() {134 bot = new ChatBot("username", "password", { client: fakeClient, triggerFactory: fakeTriggerFactory });135 var triggers = [136 { name: "trigger1", type: "type1", options: {} },137 { name: "trigger2", type: "type1" },138 { name: "trigger3", type: "type2", options: {} }139 ];140 expect(bot.addTriggers(triggers)).toEqual(true);141 expect(_.size(bot.triggers)).toEqual(3);142 });143 it("should return false if any trigger added in bulk is invalid", function() {144 bot = new ChatBot("username", "password", { client: fakeClient, triggerFactory: fakeTriggerFactory });145 var triggers = [146 { name: "trigger1", type: "type1", options: {} },147 { name: "trigger2", type: "type1" },148 { name: "trigger3", type: "invalidType", options: {} }149 ];150 expect(bot.addTriggers(triggers)).toEqual(false);151 expect(_.size(bot.triggers)).toEqual(2);152 });153 it("should be able to export trigger details and in the same form that they can be added", function() {154 bot = new ChatBot("username", "password", { client: fakeClient, triggerFactory: fakeTriggerFactory });155 var triggers = [156 { name: "trigger1", type: "type1", options: { option1: true, option2: "string", option3: 3 } },157 { name: "trigger2", type: "type1" },158 { name: "trigger3", type: "type2", options: {} }159 ];160 bot.addTriggers(triggers);161 var exportedTriggers = bot.getTriggerDetails();162 expect(_.size(exportedTriggers)).toEqual(3);163 expect(_.size(bot.triggers)).toEqual(3);164 bot.clearTriggers();165 expect(_.size(bot.triggers)).toEqual(0);166 expect(bot.addTriggers(exportedTriggers)).toEqual(true);167 expect(_.size(bot.triggers)).toEqual(3);168 var exportedTriggers = {};169 expect(_.size(bot.triggers)).toEqual(3);170 });171 });172 describe("ChatBot trigger interactions", function() {173 it("should call onChatInvite on triggers when a user chat room invitation is sent", function() {174 bot = new ChatBot("username", "password", { client: fakeClient, triggerFactory: fakeTriggerFactory });175 var fakeTrigger1 = bot.addTrigger("fakeTrigger1", "type");176 var fakeTrigger2 = bot.addTrigger("fakeTrigger2", "type");177 bot._onChatInvite("roomId", "roomName", "inviterId");178 expect(fakeTrigger1.onChatInvite).toHaveBeenCalledWith("roomId", "roomName", "inviterId");179 expect(fakeTrigger2.onChatInvite).toHaveBeenCalledWith("roomId", "roomName", "inviterId");180 });181 it("should call onFriendRequest on triggers when a friend request is sent", function() {182 bot = new ChatBot("username", "password", { client: fakeClient, triggerFactory: fakeTriggerFactory });183 var fakeTrigger1 = bot.addTrigger("fakeTrigger1", "type");184 var fakeTrigger2 = bot.addTrigger("fakeTrigger2", "type");185 bot._onRelationship("userId", steam.EFriendRelationship.RequestRecipient);186 expect(fakeTrigger1.onFriendRequest).toHaveBeenCalledWith("userId");187 expect(fakeTrigger2.onFriendRequest).toHaveBeenCalledWith("userId");188 });189 it("should not call onFriendRequest on triggers when a other relationship events occur", function() {190 bot = new ChatBot("username", "password", { client: fakeClient, triggerFactory: fakeTriggerFactory });191 var fakeTrigger = bot.addTrigger("fakeTrigger", "type");192 bot._onRelationship("userId", steam.EFriendRelationship.None);193 bot._onRelationship("userId", steam.EFriendRelationship.Blocked);194 bot._onRelationship("userId", steam.EFriendRelationship.RequestInitiator);195 bot._onRelationship("userId", steam.EFriendRelationship.RequestInitiator);196 bot._onRelationship("userId", steam.EFriendRelationship.Friend);197 bot._onRelationship("userId", steam.EFriendRelationship.Ignored);198 bot._onRelationship("userId", steam.EFriendRelationship.IgnoredFriend);199 bot._onRelationship("userId", steam.EFriendRelationship.SuggestedFriend);200 expect(fakeTrigger.onFriendRequest).not.toHaveBeenCalled();201 });202 it("should call onFriendMessage on triggers when a friend message is received", function() {203 bot = new ChatBot("username", "password", { client: fakeClient, triggerFactory: fakeTriggerFactory });204 var fakeTrigger = bot.addTrigger("fakeTrigger", "type");205 bot._onFriendMsg("userId", "message", steam.EChatEntryType.ChatMsg);206 expect(fakeTrigger.onFriendMessage).toHaveBeenCalledWith("userId", "message", false);207 });208 it("should not call onFriendMessage on triggers when another chat entry type is received in a friend chat", function() {209 bot = new ChatBot("username", "password", { client: fakeClient, triggerFactory: fakeTriggerFactory });210 var fakeTrigger = bot.addTrigger("fakeTrigger", "type");211 bot._onFriendMsg("userId", "message", steam.EChatEntryType.Typing);212 expect(fakeTrigger.onFriendMessage).not.toHaveBeenCalled();213 });214 it("should pass on the fact that a message has already been sent in onFriendMessage calls", function() {215 bot = new ChatBot("username", "password", { client: fakeClient, triggerFactory: fakeTriggerFactory });216 var fakeTrigger1 = bot.addTrigger("fakeTrigger1", "type");217 fakeTrigger1.onFriendMessage.andCallFake(function() { return false; });218 var fakeTrigger2 = bot.addTrigger("fakeTrigger2", "type");219 fakeTrigger2.onFriendMessage.andCallFake(function() { return true; });220 var fakeTrigger3 = bot.addTrigger("fakeTrigger3", "type");221 fakeTrigger3.onFriendMessage.andCallFake(function() { return false; });222 var fakeTrigger4 = bot.addTrigger("fakeTrigger4", "type");223 fakeTrigger4.onFriendMessage.andCallFake(function() { return false; });224 bot._onFriendMsg("userId", "message", steam.EChatEntryType.ChatMsg);225 expect(fakeTrigger1.onFriendMessage).toHaveBeenCalledWith("userId", "message", false);226 expect(fakeTrigger2.onFriendMessage).toHaveBeenCalledWith("userId", "message", false);227 expect(fakeTrigger3.onFriendMessage).toHaveBeenCalledWith("userId", "message", true);228 expect(fakeTrigger4.onFriendMessage).toHaveBeenCalledWith("userId", "message", true);229 });230 it("should call onChatMessage on triggers when a message is received in a chat room", function() {231 bot = new ChatBot("username", "password", { client: fakeClient, triggerFactory: fakeTriggerFactory });232 var fakeTrigger = bot.addTrigger("fakeTrigger", "type");233 bot._onChatMsg("roomId", "message", steam.EChatEntryType.ChatMsg, "chatterId");234 expect(fakeTrigger.onChatMessage).toHaveBeenCalledWith("roomId", "chatterId", "message", false, false);235 });236 it("should not call onChatMessage on triggers when another chat entry type is received in a chat room", function() {237 bot = new ChatBot("username", "password", { client: fakeClient, triggerFactory: fakeTriggerFactory });238 var fakeTrigger = bot.addTrigger("fakeTrigger", "type");239 bot._onChatMsg("roomId", "message", steam.EChatEntryType.InviteGame, "chatterId");240 expect(fakeTrigger.onChatMessage).not.toHaveBeenCalled();241 });242 it("should pass on the fact that a message has already been sent in onChatMessage calls", function() {243 bot = new ChatBot("username", "password", { client: fakeClient, triggerFactory: fakeTriggerFactory });244 var fakeTrigger1 = bot.addTrigger("fakeTrigger1", "type");245 fakeTrigger1.onChatMessage.andCallFake(function() { return false; });246 var fakeTrigger2 = bot.addTrigger("fakeTrigger2", "type");247 fakeTrigger2.onChatMessage.andCallFake(function() { return true; });248 var fakeTrigger3 = bot.addTrigger("fakeTrigger3", "type");249 fakeTrigger3.onChatMessage.andCallFake(function() { return false; });250 var fakeTrigger4 = bot.addTrigger("fakeTrigger4", "type");251 fakeTrigger4.onChatMessage.andCallFake(function() { return false; });252 bot._onChatMsg("roomId", "message", steam.EChatEntryType.ChatMsg, "chatterId");253 expect(fakeTrigger1.onChatMessage).toHaveBeenCalledWith("roomId", "chatterId", "message", false, false);254 expect(fakeTrigger2.onChatMessage).toHaveBeenCalledWith("roomId", "chatterId", "message", false, false);255 expect(fakeTrigger3.onChatMessage).toHaveBeenCalledWith("roomId", "chatterId", "message", true, false);256 expect(fakeTrigger4.onChatMessage).toHaveBeenCalledWith("roomId", "chatterId", "message", true, false);257 });258 it("should pass on the fact that the bot is muted in onChatMessage calls", function() {259 bot = new ChatBot("username", "password", { client: fakeClient, triggerFactory: fakeTriggerFactory });260 var fakeTrigger = bot.addTrigger("fakeTrigger", "type");261 bot.mute();262 bot._onChatMsg("roomId", "message", steam.EChatEntryType.ChatMsg, "chatterId");263 expect(fakeTrigger.onChatMessage).toHaveBeenCalledWith("roomId", "chatterId", "message", false, true);264 });265 });...

Full Screen

Full Screen

cleverbotTriggerSpec.js

Source:cleverbotTriggerSpec.js Github

copy

Full Screen

1var sinon = require('sinon');2var CleverbotTrigger = require('../lib/triggers/cleverbotTrigger.js');3describe("CleverbotTrigger", function() {4 var fakeBot;5 beforeEach(function() {6 fakeBot = jasmine.createSpyObj('fakeBot', ['sendMessage']);7 });8 it("should only respond to friend messages when a keyword is mentioned", function() {9 var fakeCleverbot = jasmine.createSpyObj('cleverbot', ['write']);10 var trigger = CleverbotTrigger.create("cleverbotTrigger", fakeBot, { keywords: ['cleverbot'], cleverbot: fakeCleverbot } );11 expect(trigger.onFriendMessage('userId', 'keyword not mentioned', false)).toEqual(false);12 expect(fakeCleverbot.write).not.toHaveBeenCalled();13 expect(trigger.onFriendMessage('userId', 'whole keyword not mentioned acleverbot', false)).toEqual(false);14 expect(fakeCleverbot.write).not.toHaveBeenCalled();15 expect(trigger.onFriendMessage('userId', 'cleverbot mentioned', false)).toEqual(true);16 expect(fakeCleverbot.write).toHaveBeenCalled();17 expect(trigger.onFriendMessage('userId', 'mentioned cleverbot', false)).toEqual(true);18 expect(fakeCleverbot.write.calls.length).toEqual(2);19 expect(trigger.onFriendMessage('userId', 'mentioned cleverbot again', false)).toEqual(true);20 expect(fakeCleverbot.write.calls.length).toEqual(3);21 });22 it("should only respond to chat messages when a keyword is mentioned", function() {23 var fakeCleverbot = jasmine.createSpyObj('cleverbot', ['write']);24 var trigger = CleverbotTrigger.create("cleverbotTrigger", fakeBot, { keywords: ['cleverbot'], cleverbot: fakeCleverbot } );25 expect(trigger.onChatMessage('roomId', 'userId', 'keyword not mentioned', false, false)).toEqual(false);26 expect(fakeCleverbot.write).not.toHaveBeenCalled();27 expect(trigger.onChatMessage('roomId', 'userId', 'whole keyword not mentioned acleverbot', false, false)).toEqual(false);28 expect(fakeCleverbot.write).not.toHaveBeenCalled();29 expect(trigger.onChatMessage('roomId', 'userId', 'cleverbot mentioned', false, false)).toEqual(true);30 expect(fakeCleverbot.write).toHaveBeenCalled();31 expect(trigger.onChatMessage('roomId', 'userId', 'mentioned cleverbot', false, false)).toEqual(true);32 expect(fakeCleverbot.write.calls.length).toEqual(2);33 expect(trigger.onChatMessage('roomId', 'userId', 'mentioned cleverbot again', false, false)).toEqual(true);34 expect(fakeCleverbot.write.calls.length).toEqual(3);35 });36 it("should not respond to messages only containing the keyword", function() {37 var fakeCleverbot = jasmine.createSpyObj('cleverbot', ['write']);38 var trigger = CleverbotTrigger.create("cleverbotTrigger", fakeBot, { keywords: ['cleverbot'], cleverbot: fakeCleverbot } );39 expect(trigger.onFriendMessage('userId', 'cleverbot', false)).toEqual(false);40 expect(fakeCleverbot.write).not.toHaveBeenCalled();41 expect(trigger.onFriendMessage('userId', ' cleverbot', false)).toEqual(false);42 expect(fakeCleverbot.write).not.toHaveBeenCalled();43 expect(trigger.onFriendMessage('userId', 'cleverbot ', false)).toEqual(false);44 expect(fakeCleverbot.write).not.toHaveBeenCalled();45 expect(trigger.onFriendMessage('userId', ' cleverbot ', false)).toEqual(false);46 expect(fakeCleverbot.write).not.toHaveBeenCalled();47 });48 it("should respond to any keyword", function() {49 var fakeCleverbot = jasmine.createSpyObj('cleverbot', ['write']);50 var trigger = CleverbotTrigger.create("cleverbotTrigger", fakeBot, { keywords: ['cleverbot', 'siri'], cleverbot: fakeCleverbot } );51 expect(trigger.onFriendMessage('userId', 'keyword not mentioned', false)).toEqual(false);52 expect(fakeCleverbot.write).not.toHaveBeenCalled();53 expect(trigger.onFriendMessage('userId', '1st keyword cleverbot mentioned', false)).toEqual(true);54 expect(fakeCleverbot.write).toHaveBeenCalled();55 expect(trigger.onFriendMessage('userId', '2nd keyword siri mentioned', false)).toEqual(true);56 expect(fakeCleverbot.write.calls.length).toEqual(2);57 });58 it("should respond to all messages when no keyword option is passed", function() {59 var fakeCleverbot = jasmine.createSpyObj('cleverbot', ['write']);60 var trigger = CleverbotTrigger.create("cleverbotTrigger", fakeBot, { cleverbot: fakeCleverbot } );61 expect(trigger.onFriendMessage('userId', 'hello', false)).toEqual(true);62 expect(fakeCleverbot.write).toHaveBeenCalled();63 });64 it("should strip out the keyword", function() {65 var fakeCleverbot = jasmine.createSpyObj('cleverbot', ['write']);66 var trigger = CleverbotTrigger.create("cleverbotTrigger", fakeBot, { keywords: ['cleverbot'], cleverbot: fakeCleverbot } );67 expect(trigger.onFriendMessage('userId', 'cleverbot should be stripped', false)).toEqual(true);68 expect(fakeCleverbot.write.calls.length).toEqual(1);69 expect(fakeCleverbot.write.calls[0].args[0]).toEqual(" should be stripped");70 expect(trigger.onFriendMessage('userId', 'stripped, should be cleverbot', false)).toEqual(true);71 expect(fakeCleverbot.write.calls.length).toEqual(2);72 expect(fakeCleverbot.write.calls[1].args[0]).toEqual("stripped, should be ");73 expect(trigger.onFriendMessage('userId', 'strip cleverbot ok', false)).toEqual(true);74 expect(fakeCleverbot.write.calls.length).toEqual(3);75 expect(fakeCleverbot.write.calls[2].args[0]).toEqual("strip ok");76 });77 it("should strip out the keyword", function() {78 var fakeCleverbot = jasmine.createSpyObj('cleverbot', ['write']);79 var trigger = CleverbotTrigger.create("cleverbotTrigger", fakeBot, { keywords: ['cleverbot'], cleverbot: fakeCleverbot } );80 expect(trigger.onFriendMessage('userId', 'cleverbot should be stripped', false)).toEqual(true);81 expect(fakeCleverbot.write.calls.length).toEqual(1);82 expect(fakeCleverbot.write.calls[0].args[0]).toEqual(" should be stripped");83 expect(trigger.onFriendMessage('userId', 'stripped, should be cleverbot', false)).toEqual(true);84 expect(fakeCleverbot.write.calls.length).toEqual(2);85 expect(fakeCleverbot.write.calls[1].args[0]).toEqual("stripped, should be ");86 expect(trigger.onFriendMessage('userId', 'strip cleverbot ok', false)).toEqual(true);87 expect(fakeCleverbot.write.calls.length).toEqual(3);88 expect(fakeCleverbot.write.calls[2].args[0]).toEqual("strip ok");89 });90 it("multiple not respond if another trigger has already fired", function() {91 var fakeCleverbot = jasmine.createSpyObj('cleverbot', ['write']);92 var trigger = CleverbotTrigger.create("cleverbotTrigger", fakeBot, { keywords: ['cleverbot'], cleverbot: fakeCleverbot } );93 expect(trigger.onFriendMessage('userId', 'hi cleverbot', true)).toEqual(false);94 expect(fakeBot.sendMessage).not.toHaveBeenCalled();95 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var fake = sinon.fake();3fake();4var sinon = require('sinon');5var stub = sinon.stub();6stub();7var sinon = require('sinon');8var spy = sinon.spy();9spy();10var sinon = require('sinon');11var mock = sinon.mock();12mock();13var sinon = require('sinon');14var sandbox = sinon.sandbox();15sandbox();16var sinon = require('sinon');17var match = sinon.match();18match();19var sinon = require('sinon');20var createStubInstance = sinon.createStubInstance();21createStubInstance();22var sinon = require('sinon');23var createSandbox = sinon.createSandbox();24createSandbox();25var sinon = require('sinon');26var fakeServer = sinon.fakeServer();27fakeServer();28var sinon = require('sinon');29var fakeServerWithClock = sinon.fakeServerWithClock();30fakeServerWithClock();31var sinon = require('sinon');32var fakeTimers = sinon.fakeTimers();33fakeTimers();34var sinon = require('sinon');35var fakeXhr = sinon.fakeXhr();36fakeXhr();37var sinon = require('sinon');38var format = sinon.format();39format();40var sinon = require('sinon');41var log = sinon.log();42log();

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var assert = require('assert');3var myObj = {4 myMethod: function() {5 return 'foo';6 }7};8var spy = sinon.spy(myObj, 'myMethod');9assert(spy.called);10var stub = sinon.stub(myObj, 'myMethod').returns('bar');11assert(stub.called);12var mock = sinon.mock(myObj);13mock.verify();14var fake = sinon.fake.returns('bar');15assert(fake.called);16var fake = sinon.fake.returns('bar');17assert(fake.called);18var fake = sinon.fake.returns('bar');19assert(fake.called);20var fake = sinon.fake.returns('bar');21assert(fake.called);22var fake = sinon.fake.returns('bar');23assert(fake.called);24var fake = sinon.fake.returns('bar');25assert(fake.called);26var fake = sinon.fake.returns('bar');27console.log(fake

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var expect = require('chai').expect;3var stub = sinon.stub();4stub.returns(42);5expect(stub()).to.equal(42);6var sinon = require('sinon');7var expect = require('chai').expect;8var stub = sinon.stub();9stub.returns(42);10expect(stub()).to.equal(42);11var sinon = require('sinon');12var expect = require('chai').expect;13var stub = sinon.stub();14stub.returns(42);15expect(stub()).to.equal(42);16var sinon = require('sinon');17var expect = require('chai').expect;18var stub = sinon.stub();19stub.returns(42);20expect(stub()).to.equal(42);21var sinon = require('sinon');22var expect = require('chai').expect;23var stub = sinon.stub();24stub.returns(42);25expect(stub()).to.equal(42);26var sinon = require('sinon');27var expect = require('chai').expect;28var stub = sinon.stub();29stub.returns(42);30expect(stub()).to.equal(42);31var sinon = require('sinon');32var expect = require('chai').expect;33var stub = sinon.stub();34stub.returns(42);35expect(stub()).to.equal(42);36var sinon = require('sinon');37var expect = require('chai').expect;38var stub = sinon.stub();39stub.returns(42);40expect(stub()).to.equal(42);41var sinon = require('sinon');42var expect = require('chai').expect;43var stub = sinon.stub();44stub.returns(42);45expect(stub()).to.equal(42);46var sinon = require('sinon');47var expect = require('chai').expect;48var stub = sinon.stub();49stub.returns(42);50expect(stub()).to.equal(42);51var sinon = require('sinon');52var expect = require('chai').expect;53var stub = sinon.stub();54stub.returns(42);

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var assert = require('assert');3var myObj = {4 add: function(a, b) {5 return a + b;6 }7};8describe('fake', function() {9 it('should return 3', function() {10 var fake = sinon.fake.returns(3);11 var result = myObj.add(1, 2);12 assert.equal(result, 3);13 });14});15var sinon = require('sinon');16var assert = require('assert');17var myObj = {18 add: function(a, b) {19 return a + b;20 }21};22describe('stub', function() {23 it('should return 3', function() {24 var stub = sinon.stub(myObj, 'add').returns(3);25 var result = myObj.add(1, 2);26 assert.equal(result, 3);27 });28});29var sinon = require('sinon');30var assert = require('assert');31var myObj = {32 add: function(a, b) {33 return a + b;34 }35};36describe('spy', function() {37 it('should return 3', function() {38 var spy = sinon.spy(myObj, 'add');39 var result = myObj.add(1, 2);40 assert.equal(result, 3);41 assert(spy.calledOnce);42 });43});44var sinon = require('sinon');45var assert = require('assert');46var myObj = {47 add: function(a, b) {48 return a + b;49 }50};51describe('mock', function() {52 it('should return 3', function() {53 var mock = sinon.mock(myObj);54 mock.expects('add').once().returns(3);55 var result = myObj.add(1, 2);56 assert.equal(result, 3);57 mock.verify();58 });59});60var sinon = require('sinon');61var assert = require('assert');62var myObj = {63 add: function(a, b) {64 return a + b;65 }66};67describe('fake', function() {

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require(‘sinon’);2var fake = sinon.fake();3fake();4expect(fake.callCount).to.equal(1);5expect(fake.called).to.be.true;6expect(fake.calledOnce).to.be.true;7expect(fake.calledTwice).to.be.false;8expect(fake.calledThrice).to.be.false;9expect(fake.calledBefore(fake)).to.be.false;10expect(fake.calledAfter(fake)).to.be.false;11expect(fake.calledOn(fake)).to.be.true;12expect(fake.calledWithNew()).to.be.false;13expect(fake.calledWithNew()).to.be.false;14expect(fake.calledWith()).to.be.false;15expect(fake.calledWith(1, 2, 3)).to.be.false;16expect(fake.calledWithExactly(1, 2, 3)).to.be.false;17expect(fake.calledWithMatch(1, 2, 3)).to.be.false;18expect(fake.calledWithMatch(1, 2, 3)).to.be.false;19expect(fake.alwaysCalledWith(1, 2, 3)).to.be.false;20expect(fake.alwaysCalledWithExactly(1, 2, 3)).to.be.false;21expect(fake.alwaysCalledWithMatch(1, 2, 3)).to.be.false;22expect(fake.neverCalledWith(1, 2, 3)).to.be.false;23expect(fake.neverCalledWithMatch(1, 2, 3)).to.be.false;24expect(fake.threw()).to.be.false;25expect(fake.threw(‘TypeError’)).to.be.false;26expect(fake.threw(‘TypeError’, ‘message’)).to.be.false;27expect(fake.returned()).to.be.false;28expect(fake.returned(‘foo’)).to.be.false;29expect(fake.firstCall).to.be.an.instanceOf(sinon.Call);30expect(fake.lastCall).to.be.an.instanceOf(sinon.Call);31expect(fake.getCall(0)).to.be.an.instanceOf(sinon.Call);32expect(fake.calledBefore(fake)).to.be.false;33expect(fake.calledAfter(fake)).to.be.false;34expect(fake.calledImmediatelyBefore(fake)).to.be.false;35expect(fake.calledImmediatelyAfter(fake)).to.be.false;36expect(fake.firstCall.calledBefore(fake)).to.be.false;37expect(fake.firstCall.calledAfter(fake)).to.be.false;38expect(fake.firstCall.calledImmediatelyBefore(fake)).to.be.false;39expect(fake.firstCall.calledImmediatelyAfter(fake)).to.be.false;40expect(fake.lastCall.calledBefore(fake)).to.be

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require("sinon");2var fake = sinon.fake();3var sinon = require("sinon");4var stub = sinon.stub();5var sinon = require("sinon");6var spy = sinon.spy();7var sinon = require("sinon");8var mock = sinon.mock();9var sinon = require("sinon");10var sandbox = sinon.createSandbox();11var sinon = require("sinon");12var fake = sinon.fake();13var sinon = require("sinon");14var stub = sinon.stub();

Full Screen

Using AI Code Generation

copy

Full Screen

1sinon.stub(ajax, 'ajax').yieldsTo('success', {name: 'foo'});2sinon.stub(ajax, 'ajax').yieldsTo('error', 'error');3sinon.stub(ajax, 'ajax').yieldsTo('complete', 'complete');4sinon.stub(ajax, 'ajax').yieldsTo('progress', 'progress');5sinon.stub(ajax, 'ajax').yieldsTo('timeout', 'timeout');6sinon.stub(ajax, 'ajax').yieldsTo('abort', 'abort');7sinon.stub(ajax, 'ajax').yieldsTo('readystatechange', 'readystatechange');8sinon.stub(ajax, 'ajax').yieldsTo('load', 'load');9sinon.stub(ajax, 'ajax').yieldsTo('loadend', 'loadend');10sinon.stub(ajax, 'ajax').yieldsTo('loadstart', 'loadstart');11sinon.stub(ajax, 'ajax').yieldsTo('progress', 'progress');12sinon.stub(ajax, 'ajax').yieldsTo('timeout', 'timeout');13sinon.stub(ajax, 'ajax').yieldsTo('error', 'error');14sinon.stub(ajax, 'ajax').yieldsTo('abort', 'abort');15sinon.stub(ajax, 'ajax').yieldsTo('readystatechange', 'readystatechange');

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var chai = require('chai');3var expect = chai.expect;4var test = require('../test.js');5describe('sinon fake', function() {6 it('should call fake function', function() {7 var fake = sinon.fake.returns('fake');8 var result = test(fake);9 expect(result).to.equal('fake');10 });11});12module.exports = function (fake) {13 return fake();14};

Full Screen

Using AI Code Generation

copy

Full Screen

1var sinon = require('sinon');2var assert = require('assert');3var fs = require('fs');4var test = require('./testmodule');5var testObj = new test();6var stub = sinon.stub(fs, 'readFile').callsFake(function (path, cb) {7 cb(null, 'hello');8});9testObj.test(function (err, data) {10 assert.equal(data, 'hello');11});12var fs = require('fs');13var Test = function () {14};15Test.prototype.test = function (cb) {16 fs.readFile('test.txt', function (err, data) {17 cb(err, data);18 });19};20module.exports = Test;

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run sinon automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful