• Mocking AngularJS promise callback params in Jasmine tests

    Mocking AngularJS promise callback params in Jasmine tests

    You could argue that in a scenario such as this one, you should ideally have a separate, stand-alone callback function to handle the success response of a promise, but this post is not about that. It’s about an occasional need to mock a promise and perhaps it’s callback params in Jasmine unit-tests and this is a simple example of how it can be done.

    This is an example of a function you might find in an AngularJS controller (written in TypeScript, but doesn’t matter, it would be almost identical in JS).

    It contains a call to a service function which in turn returns a promise. That promise returns a successResponse and that’s what we’re about to mock. What we want to test is a situation where a $http request didn’t fail, but it didn’t return any data either. In that case, we might want to display a toast message and skip refreshing the local cache.

    The following Jasmine test explains the code in-line. Two crucial parts are creation of the promise and how to resolve it and forcing the angular digest cycle with $rootScope.$apply(). The $apply() is needed because we’re invoking the promise from the Jasmine side, which is sort of “from the outside” and AngularJS will not be aware of that the event occurred so we need to let it know manually (the promise resolution wasn’t triggered by AngularJS itself internally).

    Hope that helps. If you know of an easier way to do this, please let me know in the comments. Thanks!

     

  • Translating canvas mouse position to JointJS matrix coordinates

    Translating canvas mouse position to JointJS matrix coordinates

    If you need a way to convert the JointJS mouse position to the canvas matrix coordinates, this is a simple way to do it (in combination with an angular mouse wheel event):

    Short, but hopefully helpful! Cheers!

  • InfluxDB Studio

    InfluxDB Studio

    It feels great when someone uses something you made and thinks it’s good. Every now and then someone creates a pull request for the InfuxData.Net lib I made and have been maintaining. It’s sort of a nice confirmation of your efforts and makes you warm and fuzzy on the inside. :D

    Well, this guy named meverett recently made a few pull requests with fixes and some nice lib improvements AND then told me that he’s actually using the library to make an InfluxDB database management studio (sort of what SQL Management Studio is for SQL, but for InfluxDB)!

    I find the admin dashboard that comes with InfluxDB a bit too simplistic and buggy and was actually thinking about how useful it would be if only there was a proper management app for Influx. Well, the InfluxDB Studio does more than the web admin dashboard and works great. And it made me really happy! :D

    This is how it looks:

    Thanks for the great work meverett!

  • Playing with IBM Quantum Computer

    Playing with IBM Quantum Computer

    What does a programmer do when he has enough free time on his hands? He tries to do something with a quantum computer. :D

    IBM will let you try and do something with their quantum computer for free. Five qubits and all! :D There is a fair bit of documentation on their site on how one should go about thinking of quantum computer programming and it involves a lot of advanced math. It gets a bit difficult to follow at times, so I guess this is where all these college math courses paid off. :D

    So, after you go through the beginners guide, you can start playing with the composer. After running a few examples from the guide and another few I found on other blogs on the internet, I realized I’m not sure what to do with all this at this stage. You can spend your computing credits quite fast and easily so it feels a bit limiting and slow to do anything. But that’s ok, it wouldn’t be right for someone to usurp the thing for too long. You also realize quite quickly that today’s quantum computers aren’t really that powerful yet. Instead it feels more like a really interesting proof of concept. So anyway, it was really cool to be able to even do that and the fact that this is actually publicly accessible to anyone is beyond awesome. Guess I’ll pay attention to what happens with this in the future. :)

    In the mean time check how this guy made the rock/paper/scissors on the platform!

  • Passed the C# 70-483 exam (MCP)

    Passed the C# 70-483 exam (MCP)

    Today, after a few weeks of preparation I passed the C# 70-483 exam and became a Microsoft Certified Professional. Woohoo! :)

    Besides getting a cert, was it worth it? I would say in case of this particular exam – yes. I was already familiar with many topics covered by the exam but it did fill some gaps in my C# knowledge. And I enjoyed learning the new bits. It also did feel like I had to learn some details by hard at some points, which was less enjoyable.

    What did I use to prepare myself? I used the official practice exams from measureup, the 70-483 ref book, the Wrox Certification Toolkit book for this exam, in a few cases the official documentation and of course – StackOverflow. I feel the practice exam was very beneficial, the official ref book was also helpful but in my opinion very badly written. It’s as if the book has no structure and as if the authors didn’t communicate between themselves so that would make sense and so you could learn in some kind of logical order. Instead, the book jumps between topics in an unexpected way. The Wrox book was ok. I didn’t go through it in full detail, but read a few bits and pieces where I felt I wanted to get more details which I haven’t been satisfied with by the ref book. Both books seem to be riddled with various mistakes in code.

    Overall, I’m very happy I studied for this exam and that I passed. If you’re considering doing it – do it, you’ll be happy to learn new things and it isn’t really that hard but be prepared to put some time in it. Good luck! :)

  • Broadcasting AngularJS events from vanilla JavaScript

    Broadcasting AngularJS events from vanilla JavaScript

    In this short post I’ll show how to invoke AngularJS from plain JavaScript. Why would you want to do this? Well, in certain edge cases you might need to catch events in plain JS but still want to handle them back inside your Angular app. For example on an SVG element mouse event..

    Say you have an Angular controller named SomeCtrl and that’s where you want to handle the event. You’ll need an element you’ll be able to target either by an id or by a class, and it needs to be scoped with a controller like this:

    Next, say you have something like onmouseover=”onMouseOver()” on one of your HTML/SVG elements:

    You’ll need to add a $scope.$on listener for the “mouse-over-event” event inside your SomeCtrl controller. Your Angular app will catch the broadcast, you’ll be back inside the Angular scope and digest loop and you’ll be able to handle the event further from there.

    That’s it!

Back to top