Posts

How to Verify Observables are Unsubscribed to in Angular Unit Tests

In most cases, when you subscribe to an Observable , you should also unsubscribe  from it. In Angular, there are a few exceptions (for example, Observables returned by HttpClient ), but usually unsubscribing is the way to go. But how would you write a unit test to make sure this happens? One way is by using a  Subject . A Subject is a kind of Observable. It includes an observed  property which is a boolean and indicates whether or not it is being subscribed to. Prior to this, there was an observers  property which provided a count of observers, but this has been deprecated. In the test below, the component being tested contains code to ensure that JobListenerService.jobCompleted  Observable is being unsubscribed to when the component's ngOnDestroy()  method is called. The mock of the service returns a Subject  (a type of Observable), and we use that Subject to verify that it's being observed before the component is destroyed, but no longer being observ...

Architectural Considerations for Angular Applications

Image
When it comes to application architecture, my approach is holistic: it's not just about the application design, but the things around  the application -- for example, unit tests and linting -- that can help produce a solid application.  Here are some of my thoughts for creating successful Angular applications, though many of these apply to other sorts of applications as well. The goal is to design something in such a way that it's easy to maintain, easy to enhance, avoids pitfalls, and most importantly, works as intended . Here we go! 1. Use those spec files Angular was designed from the start with unit testing in mind. But a lot of Angular developers I've spoken to don't use the spec files the CLI creates for us. Having good code coverage is important, and it's worth taking the time to write those unit tests. For more information on how unit testing has helped me write better software, check out  this post. 2. Organize code into judicious feature modules with lazy-...

Meta Quest Pro Lens Issue

Image
The Problem Meta Doesn't Want You to Know About...and The Worst Customer Support Ever The Problem When using the Meta Quest Pro with a game or app taking place in a dark environment, yellow / brown / auburn colored haze appears along the outer edge of the lenses (more so, seemingly, on the right lens, and more near the top of the lens). In a bright game or app, the problem isn't visible, but when using a dark one, this problem is apparent. In the picture below, the issue can be observed most prominently near the top of the lens -- that brownish haze. It's been speculated that the issue could be backlight bleed or IPS glow, but as far as I know, neither has been confirmed. Here are some of the threads from Reddit and from Meta's Support forum discussing the issue: https://www.reddit.com/r/OculusQuest/comments/yfonqx/severe_backlight_edge_bleed_on_quest_pro_possibly/ (This one contains a "solution" that does not fix the problem.) https://communityforums.atmeta.c...

Retro Review: It Came Upon the Midnight Clear

Image
It Came Upon the Midnight Clear  is an old made-for-TV movie that I saw in syndication when I was a kid. I seem to recall it being on more than once, either multiple times during the same holiday season, or in consecutive years. I loved this movie, but looking back on it, all I remembered was that it starred Mickey Rooney as a grandfather hanging around with his grandson during the Christmas season, and the ending of the film. It doesn't appear to have gotten a home release past the VHS format, and is long out of print. I was happy to find it on YouTube recently, and watched it for the first time in ages. Even though I had fond memories of it, I had little  memory of it -- what was it about the film that I liked way back then? It was strange watching it again, as nothing was coming back to me as I did so. I'd have expected some "Oh, now I remember this part" moments, but that never happened, and by the end, I was curious as to why I had enjoyed the film so much as a ...

Brother Mark McBride, T.O.R., Remembered

Image
I recently learned of the passing of Brother Mark McBride, T.O.R.. Brother Mark was the principal of my high school, and someone I thought of as a friend. Though I hadn’t kept in touch with him, he was someone who’d cross my mind from time to time. He was a kind man, very down-to-earth, and very unlike a principal or other authority figure. On the first day of my sophomore year of high school, I arrived late and had to ask one of the faculty where I should be. I was new there that year. I didn’t know anyone, didn’t know the building, didn’t know the first-day routine. I was told that sophomores should be in the auditorium (the different grades were in different areas), and somehow found my way there. The crowd was sparse and I took a seat. A Franciscan friar was on the stage, speaking into a microphone. This was Brother Mark. The first impression he made upon me was when he said something that elicited a smattering of tepid applause — he reacted to this by saying, very dryly, “You may ...

How to Detect Route Changes in Angular

In a current project at work, we need to be able to tell what the current route is in an Angular app regardless of which component is currently active. The code below does the trick. It's probably better suited for ngOnInit() than the constructor, but this still illustrates how to do it. In this example, we're just logging the URL to the console (this was just sample code I gave to one of the developers): export class AppComponent {   constructor ( private _router : Router ) {     this . _router . events . subscribe (( event : Event ) => {       if ( event instanceof NavigationEnd )         console . log ( "CURRENT URL: " , event . url );     });   } }

How to Specify an Angular FormControl as Non-Nullable and Also Include Validation with Typed Forms

I recently upgraded a project to Angular 14. It's been a long running project, created using Angular 4 (I think), and even though I've been upgrading the version of Angular as new versions are released, I only recently learned that the  tsconfig.json  settings were the same as they'd been when the project was created, so I took the opportunity to bring those up to the current standards as well. Unfortunately, with the stricter template type-checking in place, some of my existing code no longer compiled. I figured that this would be a great time to switch over to the new Typed Forms introduced in Angular 14. One thing that threw me off a bit was how I could create a FormControl that doesn't allow nulls, but that also includes validation. The official docs mention supplying the nonNullable  option, but I was confused as to how I could also include my regular validation functions as there didn't seem to be an overload that supported that. But by digging a little deeper...