<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Abdul Rafehi on Medium]]></title>
        <description><![CDATA[Stories by Abdul Rafehi on Medium]]></description>
        <link>https://medium.com/@abdul_74410?source=rss-ee7b01305e04------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*_m966DMc9nrY0714pET-Zw.jpeg</url>
            <title>Stories by Abdul Rafehi on Medium</title>
            <link>https://medium.com/@abdul_74410?source=rss-ee7b01305e04------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 07:39:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@abdul_74410/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Instant Autocomplete: How we fixed search performance]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://itnext.io/instant-autocomplete-how-we-fixed-search-performance-63d38761c80c?source=rss-ee7b01305e04------2"><img src="https://cdn-images-1.medium.com/max/700/0*ummLsewhO4EzSjLq.png" width="700"></a></p><p class="medium-feed-snippet">In an era of instant gratification, 2 seconds can feel like a lifetime.</p><p class="medium-feed-link"><a href="https://itnext.io/instant-autocomplete-how-we-fixed-search-performance-63d38761c80c?source=rss-ee7b01305e04------2">Continue reading on ITNEXT »</a></p></div>]]></description>
            <link>https://itnext.io/instant-autocomplete-how-we-fixed-search-performance-63d38761c80c?source=rss-ee7b01305e04------2</link>
            <guid isPermaLink="false">https://medium.com/p/63d38761c80c</guid>
            <category><![CDATA[database]]></category>
            <category><![CDATA[data]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Abdul Rafehi]]></dc:creator>
            <pubDate>Tue, 03 Jan 2023 05:33:33 GMT</pubDate>
            <atom:updated>2023-01-04T08:21:26.839Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Improving Query Performance by 10000x]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/better-programming/improving-query-performance-by-10000x-79b84c80fbaf?source=rss-ee7b01305e04------2"><img src="https://cdn-images-1.medium.com/max/2600/0*iZaGiPHhI3k9g3jW" width="3540"></a></p><p class="medium-feed-snippet">Boost the speed of your&#xA0;system</p><p class="medium-feed-link"><a href="https://medium.com/better-programming/improving-query-performance-by-10000x-79b84c80fbaf?source=rss-ee7b01305e04------2">Continue reading on Better Programming »</a></p></div>]]></description>
            <link>https://medium.com/better-programming/improving-query-performance-by-10000x-79b84c80fbaf?source=rss-ee7b01305e04------2</link>
            <guid isPermaLink="false">https://medium.com/p/79b84c80fbaf</guid>
            <category><![CDATA[data]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Abdul Rafehi]]></dc:creator>
            <pubDate>Sat, 02 Apr 2022 09:52:24 GMT</pubDate>
            <atom:updated>2022-04-05T10:46:47.755Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Unit Testing Spring Method Security]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/swlh/unit-testing-spring-method-security-ca85c026046c?source=rss-ee7b01305e04------2"><img src="https://cdn-images-1.medium.com/max/600/0*noBDrCAvreJSzOSi.png" width="600"></a></p><p class="medium-feed-snippet">How to test your application to make sure its properly secured</p><p class="medium-feed-link"><a href="https://medium.com/swlh/unit-testing-spring-method-security-ca85c026046c?source=rss-ee7b01305e04------2">Continue reading on The Startup »</a></p></div>]]></description>
            <link>https://medium.com/swlh/unit-testing-spring-method-security-ca85c026046c?source=rss-ee7b01305e04------2</link>
            <guid isPermaLink="false">https://medium.com/p/ca85c026046c</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[spring-security]]></category>
            <dc:creator><![CDATA[Abdul Rafehi]]></dc:creator>
            <pubDate>Sat, 20 Jun 2020 06:51:47 GMT</pubDate>
            <atom:updated>2020-06-22T06:07:22.493Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Angular Testing Tips and Tricks]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://itnext.io/angular-testing-tips-and-tricks-6c1e21a7cf65?source=rss-ee7b01305e04------2"><img src="https://cdn-images-1.medium.com/max/1920/1*YayObskHgB9y7f-pWu4daQ.png" width="1920"></a></p><p class="medium-feed-snippet">Tips and tricks to help you get the most out of your tesing</p><p class="medium-feed-link"><a href="https://itnext.io/angular-testing-tips-and-tricks-6c1e21a7cf65?source=rss-ee7b01305e04------2">Continue reading on ITNEXT »</a></p></div>]]></description>
            <link>https://itnext.io/angular-testing-tips-and-tricks-6c1e21a7cf65?source=rss-ee7b01305e04------2</link>
            <guid isPermaLink="false">https://medium.com/p/6c1e21a7cf65</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Abdul Rafehi]]></dc:creator>
            <pubDate>Mon, 27 Jan 2020 23:32:40 GMT</pubDate>
            <atom:updated>2020-01-31T11:31:18.853Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Towards Better Testing In Angular. Part 3— Inputs & Outputs]]></title>
            <link>https://medium.com/@abdul_74410/towards-better-testing-in-angular-part-3-inputs-outputs-e8ed361cdad6?source=rss-ee7b01305e04------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8ed361cdad6</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[typescript]]></category>
            <dc:creator><![CDATA[Abdul Rafehi]]></dc:creator>
            <pubDate>Thu, 23 Jan 2020 05:14:17 GMT</pubDate>
            <atom:updated>2020-01-23T05:14:17.567Z</atom:updated>
            <content:encoded><![CDATA[<h4>Tips and best practices on how to build better, more robust Angular applications</h4><p><em>This is Part 3 of the series. </em><a href="https://medium.com/@abdul_74410/towards-better-testing-in-angular-part-1-mocking-child-components-b51e1fd571da"><em>Check out part 1</em></a><em> and </em><a href="https://medium.com/@abdul_74410/towards-better-testing-in-angular-part-2-service-injection-c87b1fede954"><em>part 2</em></a><em>.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4AZvDjgyrodZTEjUl_tacg.png" /></figure><h3>Introduction</h3><p><a href="https://medium.com/@abdul_74410/towards-better-testing-in-angular-part-2-service-injection-c87b1fede954">In the previous article</a>, we created and tested a simple Todo app. The different component in the app interacted through a shared service. In this article, we’ll reimplement the Todo app, this time using Inputs and Outputs. We’ll demonstrate how using Inputs and Outputs lead to an application that’s both easier to test and reason about.</p><h3>What are the issues with service injection?</h3><p>We’ve implemented a working todo list. The tests are relatively straight-forward, the code is simple, and refactoring doesn’t seem like it’ll be a huge amount of effort. The question is, are there any improvements we can make?</p><h4>The components are tightly coupled to the service.</h4><p>All of our components interact with the service. If we want to change the public API of the TodoService, it’ll require changing all the components that are dependent on it. All our components have full access to all the functionality provided by the service. This makes it difficult to reason about where state changes are occurring in the applications.</p><h4>All of our components are smart.</h4><p>All of our components interact with the “outside world” via the service. What’s more, both child components are impure — they change the state of the application by deleting, creating and toggling todos. In other words, they cause <em>side effects</em>. Again, this makes it difficult to reason about how the application works. It also makes testing problematic. It’s not an issue in this small application, but imagine an app with 100 components, some of which are injecting 4 or 5 services. It quickly becomes a nightmare mocking out the individual services, and the interactions between the services.</p><h4>Our application is bottom heavy.</h4><p>By this, I mean that most of the state changes and logic are occurring in the child components. As the application grows, there’ll be significantly more “child” components than parent components. Continuing to code in this style, the majority of these components may directly change the state of a service.</p><p>Let’s imagine that we extended our application to allow viewing individual todos, editing them and adding notes to them (see below). This is still a small application with only 8 components, <em>but already 7 of those components are injecting the service</em>. What’s more, all the state changes are occurring in the child components. When state changes occur in the majority of your components, refactoring and reasoning become very difficult.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/839/1*PzdQd9ajw-kI733REUZU5w.png" /></figure><h3>Can we do better?</h3><p>Turns out, the answer is yes. Let’s consider each of the issues raised in the previous section and see how we can fix them. Fortunately, all 3 issues can be solved by making our child components dumb.</p><p><strong>Only inject services in the parent components. </strong>If we limit service injection to only the parent components, we’re decoupling the majority of our application from the service. If the service changes, only the parent component needs to change — the child components are unaffected.</p><p>We achieve this by <strong>turning our child components into dumb components</strong>. This means that instead of them invoking the service directly, they instead emit events via Outputs to their parent. The parent component is then responsible for handling these events and interacting with the service. If the children need state, they are passed in the state via Inputs, instead of retrieving it directly from the service. Our components are now pure — they don’t cause any side effects, making them much easier to reason about.</p><p><strong>This ensures our application is top-heavy. </strong>A top-heavy application is one where all the side-effects are occurring at the top of our component hierarchy, in what we call “smart components”. When reasoning about the application, we can understand how it works just by looking at the smart components. We can disregard the multitude of dumb components, as all and any state changes and side effects are occurring at the top.</p><p>We can observe this in the diagram below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/837/1*mCvWAaoAHQaAlLnxpLPUJA.png" /></figure><h3>Using Inputs and Outputs</h3><p>Let’s modify our original implementation to use Inputs and Outputs instead of service injection.</p><h4>TodoComponent</h4><p>We’ll start with TodoComponent again. This time, instead of injecting the service, we’ll create two EventEmitters, one for deleting and one for toggling:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/670/1*uSqOb3Q-LVgDsAqfNlH3zQ.png" /><figcaption>Change TodoComponent to use Outputs instead of service injection</figcaption></figure><p>Let’s set up the tests. This time, we don’t have to worry about providing a mock service.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/651/1*RfN1XCo9AA2WWzH61D2kQw.png" /><figcaption>The test setup is easier now that we don’t need to mock TodoService</figcaption></figure><p>The only tests that need to change are the ones that were previously using TodoService. We’ll change these to test that the appropriate events are being emitted when expected:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/679/1*J65SI6npukN_FFtX2MJ3Zw.png" /></figure><p>By spying on the emit function of the EventEmitters, we can verify that the events were emitted. How those events are handled isn’t the responsibility of this component — we’ve tested that it handles its inputs correctly, and emits events when we expect it to. It’s up to the parent to ensure it handles those events correctly.</p><h4>TodoCreatorComponent</h4><p>Next, we’ll also change TodoCreatorComponent to emit events. Here’s the updated code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*LIzoQCkpGTKGIZkpc59_TA.png" /></figure><p>It’s very similar to the previous iteration, except we’re now emitting a create event when the Create button is clicked. Once again, setting up the tests is simpler now that we don’t need to worry about TodoService:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*vOilm5UcNLR3QET6ZtdSjA.png" /></figure><p>As above, we’ll only need to change the tests that previously relied on the service:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/648/1*qsfYQBRAi5XE35fHse4OKw.png" /></figure><h4>TodoContainerComponent</h4><p>This component will have the most changes. It’ll need to listen to the outputs generated by its children and invoke the service accordingly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/660/1*UxboEyZ_NO4ELO_L1KOmGA.png" /></figure><p>This component is doing a lot more than previously. In the template, it subscribing to the outputs of both components then handling those by invoking the appropriate service methods. This is what we mean by top-heavy architecture. All the state changes are occurring here, at the top of the component hierarchy. It is the only component that needs to know how to interface with the rest of the application (in this case, through TodoService).</p><p>While there is more functionality here, we’re still deferring all the work to TodoService. Our components are still primarily concerned with the presentation of our todos. It is up to the service to handle the business logic of fetching, creating, toggling and deleting todos. What we’ve accomplished here is to ensure that all our state changes are happening in the same place, rather than spread out throughout the application.</p><p>Let’s set up the tests for this component. This time, we’ll need to spy on all methods of TodoService that we’ve used so far:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/652/1*6iFMfXtWgwXsHYAdnuAcFA.png" /></figure><p>This time, we’ll need to test that the service is invoked when required. As we’re using ngMocks to mock the child components, we can treat them as black boxes. We don’t care <em>when</em> the child component’s Output emits, all we need to test is that when they do emit, we handle them correctly. We’re testing that we’ve wired up the outputs of the child components correctly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/1*ldGgxLnETeA0DQ9duMTguA.png" /></figure><p>As ngMocks automatically creates the event emitters for us, all we need to do is emit the outputs from the children and test that the appropriate service function was called.</p><h3>Conclusion</h3><p>Check out the unit tests in the Stackblitz below. The service-injection folder shows Solution 1, using service injection. inputs-outputs is the second implementation, using Inputs/Outputs.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fstackblitz.com%2Fedit%2Fangular-tests-inputs-outputs%3Fembed%3D1&amp;url=https%3A%2F%2Fstackblitz.com%2Fedit%2Fangular-tests-inputs-outputs&amp;image=https%3A%2F%2Fc.staticblitz.com%2Fassets%2Ficon-664493542621427cc8adae5e8f50d632f87aaa6ea1ce5b01e9a3d05b57940a9f.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=stackblitz" width="745" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/d2cecdf1d64b1fe100ec92a090b38853/href">https://medium.com/media/d2cecdf1d64b1fe100ec92a090b38853/href</a></iframe><p>We’ve now compared service injection and Inputs/Outputs as a means of component interaction, and what the implications of each are when unit testing.</p><p><em>Any questions or feedback on the article, or requests on what you’d like me to write about next? Leave me a comment below or hit me up on twitter, </em><a href="https://twitter.com/abdul_rafehi"><em>@abdul_rafehi</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e8ed361cdad6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Towards Better Testing In Angular. Part 2 — Service Injection]]></title>
            <link>https://medium.com/@abdul_74410/towards-better-testing-in-angular-part-2-service-injection-c87b1fede954?source=rss-ee7b01305e04------2</link>
            <guid isPermaLink="false">https://medium.com/p/c87b1fede954</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Abdul Rafehi]]></dc:creator>
            <pubDate>Mon, 20 Jan 2020 13:01:01 GMT</pubDate>
            <atom:updated>2020-06-23T05:52:57.351Z</atom:updated>
            <content:encoded><![CDATA[<h3>Towards Better Testing In Angular. Part 2 — Service Injection</h3><h4>Tips and best practices on how to build better, more robust Angular applications</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qV7t2BNXsoRrPSNPp4n1_A.png" /></figure><p><em>This is Part 2 of the series. </em><a href="https://medium.com/@abdul_74410/towards-better-testing-in-angular-part-1-mocking-child-components-b51e1fd571da"><em>Check out part 1 here</em></a><em>.</em></p><h3>Introduction</h3><p>Angular is a powerful framework. It gives you the ability to accomplish things in multiple ways.</p><p>As our Friendly, Neighbourhood Uncle Ben solemnly told us:</p><blockquote><em>With great power comes great responsibility.</em></blockquote><p>This holds just as true in the Angular world as it does in the Spider-Verse. When we first begin our Angular journey, one of the first challenges we’ll come across is how to achieve interaction between components. There are three canonical ways to achieve this:</p><ul><li>Indirectly, through service injection</li><li>Directly, through Inputs and Outputs</li><li>Directly, through ViewChild / ContentChildren</li></ul><p>As is often the case, the correct answer to “which approach should I take?” is: it depends. Let’s consider a few examples:</p><ul><li>Components have a parent/child relationship: Inputs and Outputs are a good approach</li><li>Components are in separate routes: service injection might be our only choice</li><li>Components take in dynamic content: ContentChildren is the way to go</li><li>Need to call methods within a child component: we’ll have to use either ViewChild or service injection</li></ul><p>In this article, we’ll consider what is perhaps the most common case — how to interact with components that share a parent/child relationship. We’ll consider service injection in this tutorial. In the next tutorial, we’ll show how we can achieve the same result using Inputs and Outputs, while making our program easier to reason about.</p><p>We’ll look at the old stalwart of examples, the todo app. We’ll start with a decent-but-not-ideal implementation of the todo app, using service injection. There’s a working Stackblitz at the end of the article showing the tests running.</p><h3>The TodoService</h3><p>Let’s start by writing a service to handle retrieving, adding and deleting todos. In the real world, we’d be persisting the todos to a server, but here we’ll just keep a copy in memory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*1bJfSUlm3h_f_OCQDAVxQw.png" /></figure><p>Here’s what our Todo class looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*djTSYg1rvurEkmHsOGoK6w.png" /></figure><h3>Injecting Services into Child Components</h3><p>Our first solution involves injecting TodoService wherever it is required. For this solution, we’ll create three components:</p><ul><li>TodoContainerComponent. This component retrieves the todos from the TodoService</li><li>TodoComponent. This component handles the rendering of each todo. It renders the Todo title and handles toggling and deleting of each todo.</li><li>TodoCreatorComponent. This component allows the creation of new todos.</li></ul><h4>TodoComponent</h4><p>Let’s start with the TodoComponent. Here’s the finished component (with some basic styling applied):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*_PkrJLTrCgXjC7clmzJ3aw.png" /></figure><p>The code to implement is as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*FMNRosCNMLcACDQb_bd1Qg.png" /></figure><p>This component is fairly straight forward. We display the title of the todo (we strike-through completed todos by applying the .complete class). We’ve got a checkbox to toggle the completed state of the todo, and a button to delete the todo.</p><p>As we’re injecting TodoService we’ll need to mock out this dependency. This ensures that we’re isolating our tests from the implementation details of the TodoService.</p><p>We use Jasmine to mock the TodoService . First, let’s declare a variable to store the mocked service at the top of our describe block. We’ll also create an instance of todo to use in our unit tests.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/651/1*En21bVuNtgyMQuwNGzHJMw.png" /></figure><p>Next, we’ll instantiate the TodoService in the beforeEach block. We’ll need to spy on the delete and toggle methods as we’re using those in the tests.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*rY5pg_D3mKkBzcY_ytgVpw.png" /></figure><p>So we’ve created our spy service and assigned it as a provider. As a final step, let’s set the todo input of the component in the synchronous forEach block:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*4EsDeXDwfPVAIq6M7Odg9w.png" /></figure><p>We’re now ready to start testing. We’ll start by testing that our component can be created and that it is rendering the todo title correctly. We’ll also test that the complete class is applied if the todo is completed:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/682/1*0WIitG44oXAiY5IaRzn0XA.png" /></figure><p>Next, we’ll want to test that deletion and toggling works as expected. Remember back to the previous tutorial: we want to isolate our tests to make them robust and easier to reason about. All the tests should be testing is the functionality of TodoComponent. We don’t care what TodoService does, all we care about is that the methods are invoked correctly. We’re treating TodoService as a black-box.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9eJzoysyE3kvHv0Qf-x3Iw.png" /><figcaption>Testing that delete and toggle are invoked correctly</figcaption></figure><h4>TodoCreatorComponent</h4><p>Next, we’ll implement TodoCreatorComponent. The process is similar to the TodoComponent above, so I’ll go a little bit faster. This component contains an input field with a button to add create the todo:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/366/1*X1Iq0roWKDnLkorpO5xltA.png" /><figcaption>The TodoCreatorComponent</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/670/1*uonC2QnD1WNvylBadMrHbA.png" /></figure><p>Again, we’re injecting TodoService to handle the creation of new todos. We’re also using ngModel to bind the input control to our component, so make sure to include FormsModule in app.module.ts if you’re following along at home.</p><p>We start the tests by including FormsModule in our TestBed configuration. This time, we’re only spying on the add method as that’s all that is used by this component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*NvgtvtDi6kNUExKjhpj-FQ.png" /></figure><p>Let’s write some helper functions to aid with querying elements and dispatching input events:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*2p2TQUI7rOaDaK6EP3fxjw.png" /></figure><p>Let’s test that the error state works as expected:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/1*h48PyeLItC-69MU8lKZsVA.png" /></figure><p>We’ll also ensure that the TodoService is invoked correctly (again, we’ll treat it as a black-box). We want to ensure that add is not called if the title is empty, and that add is called if it is not empty:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/1*uX0DwFkIbpo6_NZ_MEYqHQ.png" /></figure><h4>TodoContainerComponent</h4><p>Finally, let’s implement TodoContainerComponent. This component contains a list of Todos as well as the TodoCreatorComponent to add more. Once done, it’ll look as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/355/1*b1Dy0WbhqEBXjnqt5zOcZA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*Ptmx4iWNe8oxVFMSUEml8w.png" /></figure><p>This component is very simple — it’s just subscribing to TodoService to get the list of todos, and using that to instantiate the TodoComponents. Let’s set up the tests:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*DNLTbX4J5oRpJTwVYGIQbA.png" /></figure><p>This time, we need to mock TodoService.todos$(), which returns an observable of Todos. To do that, we create a subject, todos$, assign it an initial state and have todoService.$todos$() return that subject. We’re also using ngMock’s MockComponent to create mock versions of TodoComponent and TodoCreatorComponent, making it much easier to test them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*I06_B6N4DSgyeu6FUdYgyA.png" /></figure><p>Here are the tests. In this case, we’re using a helper function to return all the TodoComponent instances. We test that each of the todos is shown; that the todos update when the subject outputs a new value, and that the correct ids are passed to eachTodoComponent.</p><p>Great! Now that is done, let’s see it in action:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/370/1*nX33YGWnGNP71BtB3k5Z8A.gif" /></figure><h3>Conclusion</h3><p>This wraps up this tutorial, demonstrating how to test components that are interacting through service injection. <a href="https://medium.com/@abdul_74410/towards-better-testing-in-angular-part-3-inputs-outputs-e8ed361cdad6">In the next tutorial, we achieve the same outcome by using Inputs and Outputs instead</a>. We explore the pros and cons of each approach, in our quest to improve how we test and reason about our applications.</p><h3>Conclusion</h3><p>Check out the unit tests in the Stackblitz below.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fstackblitz.com%2Fedit%2Fangular-tests-service-injection%3Fembed%3D1&amp;url=https%3A%2F%2Fstackblitz.com%2Fedit%2Fangular-tests-service-injection&amp;image=https%3A%2F%2Fc.staticblitz.com%2Fassets%2Ficon-664493542621427cc8adae5e8f50d632f87aaa6ea1ce5b01e9a3d05b57940a9f.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=stackblitz" width="745" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/42da04dd8f2ef9a6b848af1c125836e3/href">https://medium.com/media/42da04dd8f2ef9a6b848af1c125836e3/href</a></iframe><p>In this article, we showed to use and tests components interacting via services. In the next article, we’ll compare this against Inputs/Outputs as a means of component interaction, and what the implications of each are when unit testing.</p><p><em>Any questions or feedback on the article, or requests on what you’d like me to write about next? Leave me a comment below or hit me up on twitter, </em><a href="https://twitter.com/abdul_rafehi"><em>@abdul_rafehi</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c87b1fede954" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Towards Better Testing In Angular. Part 1 — Mocking Child Components]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@abdul_74410/towards-better-testing-in-angular-part-1-mocking-child-components-b51e1fd571da?source=rss-ee7b01305e04------2"><img src="https://cdn-images-1.medium.com/max/658/0*Q0RqNJiPTtau7a0J.png" width="658"></a></p><p class="medium-feed-snippet">Tips and best practices on how to build better, more robust Angular applications</p><p class="medium-feed-link"><a href="https://medium.com/@abdul_74410/towards-better-testing-in-angular-part-1-mocking-child-components-b51e1fd571da?source=rss-ee7b01305e04------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@abdul_74410/towards-better-testing-in-angular-part-1-mocking-child-components-b51e1fd571da?source=rss-ee7b01305e04------2</link>
            <guid isPermaLink="false">https://medium.com/p/b51e1fd571da</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Abdul Rafehi]]></dc:creator>
            <pubDate>Thu, 21 Feb 2019 03:45:47 GMT</pubDate>
            <atom:updated>2020-06-23T05:51:38.608Z</atom:updated>
        </item>
    </channel>
</rss>