Testing AJAX with Web Component Tester -- Polycasts #37

Chrome for Developers · Beginner ·🔧 Backend Engineering ·10y ago

Key Takeaways

Tests AJAX with Web Component Tester and Sinon.js

Full Transcript

hey there podcasters Rob here today on the show we're going to be looking at how to test elements that require Ajax to talk to their backend using web component tester that made no sense let me back up a little today on the show we're going to be continuing our series on web component tester by looking at how to test elements that require Ajax now if you go check out the documentation for web component tester you'll see up there at the top there's a list of libraries that come with web component tester itself so we've got mocha that is our testing framework uh we've got chai which is our assertion library and we'll be talking about that in a bit we got async and low Dash these are libraries they're just sort of like utilities they help us do things like manage callbacks and do fancy functional stuff and lastly we've got sinon and sinon is a little library that lets you uh mock xhr request create fake servers do really anything you'd want to do if you want to to simulate an actual server and rather than like create some brand new component or anything like that I'm just going to write unit test for a pre-existing element so I'm actually going to use one of the polymer teams elements iron Ajax and I'm just going to write test against that so that way I don't have to come up with like a weird contrived example because I'm lazy so what I'm going to do in my test file is start off by importing iron Ajax and then I'm going to write a little test fixture for it so inside this test fixture I'm calling iron Ajax or I've got a little iron Ajax tag I should say and its URL is set to responds to get with Json all right and then after that I'm going to create the suite that's going to hold all of my unit tests so I'm just going to call my sweet iron Ajax just so it's kind of like a nice General thing and I'm going to create some Global variables in here uh these are just going to keep track of the iron Ajax tag of the request as we're sending it out of the sonon server we're going to setup nothing too fancy here after that is created I'm going to call a setup function and this is where I'm going to go ahead and start to build my sinon server so I'll call sonon fake Server create that's going to return an instance of the server and then I can call this respond with method and what that's going to let me do is basically set up routes that I want the fake server to respond to so what I'm saying here is when there's a get request for the route response to get with Json I want it to respond with status code 200 I want it to send back those response headers that I created up at the top and lastly I want to send back this little Json payload that says success true yay so I've got that squared away next thing I'm going to do is create a little tear down function and in this tear down function I'm going to tell the server to restore itself which basically means like clean up after yourself you know if you if you created Global variables if there's any shared State anything like that just get rid of that so when we run the next test it'll be a nice Clean Slate okay so I've got that squared away now I can write the suite that's actually going to hold my unit tests inside of here I will call a setup function I'm going to grab my test fixture and sort it in that Ajax variable that I created and my first test here is just going to verify that the iron HX request is going out and that I'm getting a response back and that the data in that response looks correct the test message is going to be has seen defaults that love you because it does love you so my request object is going going to call Ajax generate request and then I can just call server. respond and this is kind of one of the cool Parts about working with sonon normally you would have to if this was like a real server you'd have to wait some amount of time set up an event listener do some sort of like async test thing to get that response here we can treat it like it's synchronous and that's really nice it means our tests are going to be really fast and after that I can use expect to inspect the response object that I got back now the last video I used assert to inspect my objects this time we're using expect and basically because we're using chai we have a few different assertion types that we can use a few different assertion libraries if you will you can go check out the documentation for chai maybe I should like include a link to that or something expect just another flavor of assertion really not too different what we're doing here is we're first checking that the response is okay and that means that it wasn't an error it wasn't null we got something back we're checking that it's an object right that's what we're expecting to see and lastly we're checking that the success property is set to True okay this looks okay to me uh let's switch over to the terminal and we're going to run the wct command all right things are happening things are happening and we get our test back awesome all right everything's green looking good I want to do some more testing though I want to test a PO post request so I'm going to create a brand new test fixture and this time I'm again using an iron Ajax element I'm setting the method to post and the URL is response to post with Chason just like we did for the get request we will set up a route in our sinon fake server so we're going to say that we want it to listen for post request on the route response to post the Chason and when it hears that it should send back a 200 same response headers as last time and this time the object it sends back is going to say post request is true yay over here in our test I'll write a brand new suite for making simple post requests the first test I'm actually just going to uh verify that when I set that method to post that that is actually setting the xhr method so I'm not even calling sin on here I'm just verifying that under the hood the element does what it expects after that I'm going to go ahead and actually ping the S on server now I'm going to test uh that the response is okay I'm going to test that it is an object and I'm going to test that post success is equal to True right let's switch over to our terminal we will run component tester and we will wait waiting on things to happen great so we got all of our tests are passing everything's green now what if those tests weren't passing though what if one of them failed what if it was just like one browser that failed or something weird like that what would we do this is where I'm going to show you a little trick here when we run the web component tester command we can actually pass it the- P flag and what this is going to do is it's going to leave all the browser windows open and this is really useful because it means we can open up the console for those different browsers we can inspect them and if there were errors they're going to show up there so here I've got Firefox I'm going to zoom in I'm going to open the Firefox Dev tools so I'm going to enhance this and you'll see that I've got my test listed here but if there were any errors they would show up there as well so pretty pretty helpful little feature there all right that covers testing with Ajax with sonon there's actually a lot more you can do with sinon but I've definitely given you enough to get started there's a lot more that I want to cover with weapon component tester though so if there's segments that you are interested in seeing please leave a comment down below that way we can try and make some of those episodes happen also consider clicking that like button or the Subscribe button if you truly love me and if you have some questions you can ping us on a social network of you're choosing at # askpolymer as always thank you so much for watching and I'll see you next time turn your phones [Music] off Gotham needs me response [Music] codes I got ahead of myself

Original Description

Elements that need to get data from a backend service can be tricky to test. Do you need to startup your own test server just to verify an element does what it's supposed to? Is that even a good idea? Today on Polycasts I'll show you have to use Web Component Tester and a library called Sinon.js to mock your back end and speed up your unit tests. By mocking your backend you can verify that your element's work in isolation, which is a crucial step to having good overall test coverage of your app. Web Component Tester https://github.com/Polymer/web-component-tester Iron Ajax https://github.com/PolymerElements/iron-ajax Sinon.js http://sinonjs.org/ Chai Expect http://chaijs.com/api/bdd/ Polymer Slack: http://bit.ly/polymerslack Subscribe to the Chrome Developers channel at http://goo.gl/LLLNvf
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Chrome for Developers · Chrome for Developers · 52 of 60

1 Polymer Performance Patterns (The Polymer Summit 2015)
Polymer Performance Patterns (The Polymer Summit 2015)
Chrome for Developers
2 Polymer Power Tools (The Polymer Summit 2015)
Polymer Power Tools (The Polymer Summit 2015)
Chrome for Developers
3 Chrome Dev Summit 2014 – Chrome Case Studies
Chrome Dev Summit 2014 – Chrome Case Studies
Chrome for Developers
4 Web Directions Code 2015 round up
Web Directions Code 2015 round up
Chrome for Developers
5 Maintainable Code - HTTP203
Maintainable Code - HTTP203
Chrome for Developers
6 iron-ajax… wat?! -- Polycasts #26
iron-ajax… wat?! -- Polycasts #26
Chrome for Developers
7 The Guardian - Supercharged
The Guardian - Supercharged
Chrome for Developers
8 ES2015 (next version of JavaScript), Totally Tooling Tips (S2 Ep1)
ES2015 (next version of JavaScript), Totally Tooling Tips (S2 Ep1)
Chrome for Developers
9 #AskPolymer: Rob answers all the questions ever -- Polycasts #27
#AskPolymer: Rob answers all the questions ever -- Polycasts #27
Chrome for Developers
10 The Future of JavaScript - HTTP203
The Future of JavaScript - HTTP203
Chrome for Developers
11 Data Binding 101 -- Polycasts #28
Data Binding 101 -- Polycasts #28
Chrome for Developers
12 The Guardian part 2 - Supercharged
The Guardian part 2 - Supercharged
Chrome for Developers
13 The Future of Web Audio: with Chris Wilson and Chris Lowis
The Future of Web Audio: with Chris Wilson and Chris Lowis
Chrome for Developers
14 Chrome 46: New motion-path animations, client hints and service worker improvements
Chrome 46: New motion-path animations, client hints and service worker improvements
Chrome for Developers
15 Sublime Snippets, Totally Tooling Tips (S2 Ep2)
Sublime Snippets, Totally Tooling Tips (S2 Ep2)
Chrome for Developers
16 #AskPolymer: How do you make the show? -- Polycasts #29
#AskPolymer: How do you make the show? -- Polycasts #29
Chrome for Developers
17 Critical Path CSS, Totally Tooling Tips (S2 Mini Tip #1)
Critical Path CSS, Totally Tooling Tips (S2 Mini Tip #1)
Chrome for Developers
18 Binding to Objects -- Polycasts #30
Binding to Objects -- Polycasts #30
Chrome for Developers
19 Player FM - Supercharged
Player FM - Supercharged
Chrome for Developers
20 Where’s the Designer? #AskPolymer -- Polycasts #31
Where’s the Designer? #AskPolymer -- Polycasts #31
Chrome for Developers
21 Jake Beats Wikipedia - HTTP203
Jake Beats Wikipedia - HTTP203
Chrome for Developers
22 Supercharged Observers! -- Polycasts #32
Supercharged Observers! -- Polycasts #32
Chrome for Developers
23 Jai's Web blog - Supercharged
Jai's Web blog - Supercharged
Chrome for Developers
24 Windows Command-line Tooling, Totally Tooling Tips (S2, Ep4)
Windows Command-line Tooling, Totally Tooling Tips (S2, Ep4)
Chrome for Developers
25 What about internationalization? #AskPolymer -- Polycasts #33
What about internationalization? #AskPolymer -- Polycasts #33
Chrome for Developers
26 Developing for Billions (Chrome Dev Summit 2015)
Developing for Billions (Chrome Dev Summit 2015)
Chrome for Developers
27 Google+ Performance Improvement Comparison
Google+ Performance Improvement Comparison
Chrome for Developers
28 Deploying HTTPS: The Green Lock and Beyond (Chrome Dev Summit 2015)
Deploying HTTPS: The Green Lock and Beyond (Chrome Dev Summit 2015)
Chrome for Developers
29 Progressive Web Apps (Chrome Dev Summit 2015)
Progressive Web Apps (Chrome Dev Summit 2015)
Chrome for Developers
30 Instant Loading with Service Workers (Chrome Dev Summit 2015)
Instant Loading with Service Workers (Chrome Dev Summit 2015)
Chrome for Developers
31 Increase Engagement with Web Push Notifications (Chrome Dev Summit 2015)
Increase Engagement with Web Push Notifications (Chrome Dev Summit 2015)
Chrome for Developers
32 Engaging with the Real World: Web Bluetooth and Physical Web (Chrome Dev Summit 2015)
Engaging with the Real World: Web Bluetooth and Physical Web (Chrome Dev Summit 2015)
Chrome for Developers
33 Asking for Permission: respectful, opinionated UI (Chrome Dev Summit 2015)
Asking for Permission: respectful, opinionated UI (Chrome Dev Summit 2015)
Chrome for Developers
34 Polymer - State of the Union (Chrome Dev Summit 2015)
Polymer - State of the Union (Chrome Dev Summit 2015)
Chrome for Developers
35 Building Progressive Web Apps with Polymer (Chrome Dev Summit 2015)
Building Progressive Web Apps with Polymer (Chrome Dev Summit 2015)
Chrome for Developers
36 Introduction to RAIL (Chrome Dev Summit 2015)
Introduction to RAIL (Chrome Dev Summit 2015)
Chrome for Developers
37 DevTools in 2015: Authoring to the max (Chrome Dev Summit 2015)
DevTools in 2015: Authoring to the max (Chrome Dev Summit 2015)
Chrome for Developers
38 RAIL in the real world (Chrome Dev Summit 2015)
RAIL in the real world (Chrome Dev Summit 2015)
Chrome for Developers
39 #ChromeDevSummit talks are up - W00T! -- Polycast #34
#ChromeDevSummit talks are up - W00T! -- Polycast #34
Chrome for Developers
40 V8 Performance from the Driver's Seat (Chrome Dev Summit 2015)
V8 Performance from the Driver's Seat (Chrome Dev Summit 2015)
Chrome for Developers
41 Quantify and improve real-world RAIL (Chrome Dev Summit 2015)
Quantify and improve real-world RAIL (Chrome Dev Summit 2015)
Chrome for Developers
42 Owning your performance: RAIL (Chrome Dev Summit 2015)
Owning your performance: RAIL (Chrome Dev Summit 2015)
Chrome for Developers
43 HTTP/2 101 (Chrome Dev Summit 2015)
HTTP/2 101 (Chrome Dev Summit 2015)
Chrome for Developers
44 Leadership Panel (Chrome Dev Summit 2015)
Leadership Panel (Chrome Dev Summit 2015)
Chrome for Developers
45 Build Processes, Totally Tooling Tips (S2, Ep 5)
Build Processes, Totally Tooling Tips (S2, Ep 5)
Chrome for Developers
46 Accessibility (Chrome Dev Summit 2015)
Accessibility (Chrome Dev Summit 2015)
Chrome for Developers
47 Binding to Arrays -- Polycasts #35
Binding to Arrays -- Polycasts #35
Chrome for Developers
48 HTTP2 - HTTP203
HTTP2 - HTTP203
Chrome for Developers
49 Chrome 47: Splash Screens, requestIdleCallback and better desktop notifications (New in Chrome)
Chrome 47: Splash Screens, requestIdleCallback and better desktop notifications (New in Chrome)
Chrome for Developers
50 Call For Submissions - Supercharged
Call For Submissions - Supercharged
Chrome for Developers
51 Cross Device Testing, Totally Tooling Tips (S2 Ep6)
Cross Device Testing, Totally Tooling Tips (S2 Ep6)
Chrome for Developers
Testing AJAX with Web Component Tester -- Polycasts #37
Testing AJAX with Web Component Tester -- Polycasts #37
Chrome for Developers
53 Slack: Extended Xmas Special - Supercharged
Slack: Extended Xmas Special - Supercharged
Chrome for Developers
54 Browser testing with Travis & Sauce Labs -- Polycasts #38
Browser testing with Travis & Sauce Labs -- Polycasts #38
Chrome for Developers
55 Optimize for production with Vulcanize -- Polycasts #39
Optimize for production with Vulcanize -- Polycasts #39
Chrome for Developers
56 Highlights from Chrome Dev Summit 2015
Highlights from Chrome Dev Summit 2015
Chrome for Developers
57 Chrome 48: Custom buttons in notifications, DevTools Security panel, and Presentation mode
Chrome 48: Custom buttons in notifications, DevTools Security panel, and Presentation mode
Chrome for Developers
58 Crisper: Protecting your Polymer app with CSP -- Polycasts #40
Crisper: Protecting your Polymer app with CSP -- Polycasts #40
Chrome for Developers
59 How do I use Sass with Polymer? #AskPolymer -- Polycasts #41
How do I use Sass with Polymer? #AskPolymer -- Polycasts #41
Chrome for Developers
60 Colors – DevTools Tonight #0 (Pilot)
Colors – DevTools Tonight #0 (Pilot)
Chrome for Developers

Related Reads

Up next
Beginners Guide to GPT4 API & ChatGPT 3.5 Turbo API Tutorial
Adrian Twarog
Watch →