<?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 Fabian Birke on Medium]]></title>
        <description><![CDATA[Stories by Fabian Birke on Medium]]></description>
        <link>https://medium.com/@Fabian_Birke?source=rss-59927c1fed9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*E1X2hEfRVPQ8io4Hanu0hg.jpeg</url>
            <title>Stories by Fabian Birke on Medium</title>
            <link>https://medium.com/@Fabian_Birke?source=rss-59927c1fed9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 07:57:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@Fabian_Birke/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[Angular RxJS — Replay Streams — Tip 2]]></title>
            <link>https://medium.com/@Fabian_Birke/angular-rxjs-replay-streams-tip-2-118cce27335d?source=rss-59927c1fed9------2</link>
            <guid isPermaLink="false">https://medium.com/p/118cce27335d</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[reactive-programming]]></category>
            <category><![CDATA[streaming]]></category>
            <category><![CDATA[rxjs]]></category>
            <category><![CDATA[rxjs-operators]]></category>
            <dc:creator><![CDATA[Fabian Birke]]></dc:creator>
            <pubDate>Sun, 03 Dec 2023 07:35:47 GMT</pubDate>
            <atom:updated>2023-12-03T07:35:47.811Z</atom:updated>
            <content:encoded><![CDATA[<h3>Angular RxJS — Replay Streams — Tip 2</h3><h4>With this short RxJS helper function you can rerun a RxJS stream. A typical approach in web development is the following:</h4><ul><li><strong>Fetch data </strong>e.g. GET Person (ID=1) and show them.</li><li><strong>Update data </strong>e.g. POST Person (ID=1) and click save.</li><li><strong>Re-fetch data</strong> e.g. GET Person (ID=1) and show the updated person.</li></ul><p>As the backend acts as single source of truth we can guarantee that the data we show the user is up-to-date.</p><figure><img alt="“Replay Angular Observable Stream with ease” by Fabian Birke" src="https://cdn-images-1.medium.com/max/1024/1*GbndY0VaGbNwyw2yP-KDng.jpeg" /></figure><h3>Example</h3><p>Lets have a look at the following example.</p><pre>@Component(...)<br>export class PersonComponent {<br>  public personId$ = of(1);<br><br>  public refreshPerson = new Subject&lt;void&gt;();<br><br>  constructor(private personService: PersonService) {}<br><br>  public person$ = this.personId$.pipe(<br>    withUpdater(this.refreshPerson),<br>    switchMap((personId) =&gt; this.personService.getPerson(personId))<br>  );<br><br>  public save() {<br>    // some save operations<br>    // update the stream after successful save<br>    this.refreshPerson.next();<br>  }<br>}</pre><p>We have an external service to fetch a person. In the PersonComponent we have a person$ variable, which represents the state of the person. It depends on the personId$ and fetches the data from the person service. Furthermore we are using the withUpdater function which we implement later. We pass in a subject, to trigger the replay of the stream.</p><p>And we have a save function which is called when the user saves the person. It handles the save operation and emits a value in a subject when its done.</p><h3>Custom RxJS withUpdater()-Function</h3><pre>import {combineLatest, map, MonoTypeOperatorFunction, Observable, startWith} from &#39;rxjs&#39;;<br><br>/**<br> * RxJS Operator which triggers a rerun of the incoming stream.<br> * @param $updater {Subject}<br> */<br>export const withUpdater =<br>  &lt;T&gt;($updater: Observable&lt;any&gt;): MonoTypeOperatorFunction&lt;T&gt; =&gt;<br>  ($source) =&gt;<br>   combineLatest([$source, $updater.pipe(startWith(null))]).pipe(map(([source, _]) =&gt; source));</pre><p>The withUpdater takes the input from the pipe and the parameter of the function which is aSubject and use them in a combineLatest.</p><blockquote>Keep in mind that a combineLatest only emits a value when all incoming streams emit at least one value. And after this, it emits every time one of the incoming streams emit.</blockquote><p>Therefore we need a startWith(null) for the initial case. At the end we just return the result, as we don’t need the result of the subject anymore.</p><h3>Conclusion</h3><p>This example demonstrates how you can rerun your observable streams easily. Especially if another develop will read your code. It is easy to understand, because of <strong>self-explanatory</strong> names.</p><p>You can find the full example at <a href="https://stackblitz.com/edit/stackblitz-starters-xnczdx?file=src%2Fperson%2Fperson.component.ts">StackBlitz</a>.</p><p>❤ Clap. Follow and Happy Coding ❤.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=118cce27335d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Optimizing Angular Test-Setup: A Better Approach Than beforeEach()]]></title>
            <link>https://blog.stackademic.com/optimizing-angular-test-setup-a-better-approach-than-beforeeach-bac41b7996a8?source=rss-59927c1fed9------2</link>
            <guid isPermaLink="false">https://medium.com/p/bac41b7996a8</guid>
            <category><![CDATA[karma]]></category>
            <category><![CDATA[advanced]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Fabian Birke]]></dc:creator>
            <pubDate>Sat, 02 Dec 2023 17:14:44 GMT</pubDate>
            <atom:updated>2026-08-19T12:02:31.634Z</atom:updated>
            <content:encoded><![CDATA[<h4>Streamlining Angular component testing by simplifying environment setup. Explore an alternative to the traditional ‘beforeEach’ approach for more efficient testing module initialization, enhancing flexibility across test cases</h4><h3>Introduction</h3><p>Testing Angular components often involves setting up a complex environment with mocked interfaces, making it challenging to maintain flexibility across test cases. In this blog post, we’ll explore an alternative to the traditional use of beforeEach in Angular testing, introducing a more efficient method for initializing the testing module.</p><figure><img alt="“Optimizing Angular Test-Setup: A better approach than beforeEach()” by Fabian Birke" src="https://cdn-images-1.medium.com/max/1024/1*ymMTeaugv7WyVoHnSWKMfg.jpeg" /></figure><h3>The Problem with beforeEach</h3><p>The typical beforeEach approach can become inflexible when dealing with mocked interfaces in Angular testing. Especially when the mocked data changes from test to test. To address this, we&#39;ll delve into a more streamlined solution that utilizes a separate init method.</p><h3>The Improved Approach: Using init Function</h3><p>We’ll begin by creating mock implementations for external dependencies, such as BankAccountApiService and PersonApiService. These are necessary as we don’t want to talk to the real services inside the test.</p><pre>class BankAccountApiServiceMock {<br>  getBankAccount() {<br>    return of({<br>      balance: 50_000<br>    });<br>  }<br>}<br><br>class PersonApiServiceMock {<br>  getPerson() {<br>    return of({<br>      age: 18, name: &#39;Random&#39;, hobby: &#39;tennis&#39;<br>    });<br>  }<br>}</pre><p>The two mock classes just return an observable of data. Similar to a normal backend would do.</p><h3>Streamlining Test Setup</h3><p>Instead of relying on beforeEach, we introduce an init function within our describe block. This function accepts a setup function as an optional parameter, providing the flexibility to override external dependencies for each test case.</p><p>Lets have a look.</p><pre><br>  async function init(setup?: ({bankAccountApiService, personApiService}:<br>                                 {<br>                                   bankAccountApiService: BankAccountApiService,<br>                                   personApiService: PersonApiService<br>                                 }) =&gt; void) {<br>    let bankAccountApiService = new BankAccountApiServiceMock();<br>    let personApiService = new PersonApiServiceMock();<br><br>    if (setup) {<br>      setup({bankAccountApiService, personApiService});<br>    }<br><br>    await TestBed.configureTestingModule({<br>      imports: [AppModule],<br>      providers: [<br>        {provide: BankAccountApiService, useValue: bankAccountApiService},<br>        {provide: PersonApiService, useValue: personApiService},<br>      ],<br>      schemas: [NO_ERRORS_SCHEMA]<br>    })<br>      .compileComponents();<br><br>    fixture = TestBed.createComponent(PersonComponent);<br>    component = fixture.componentInstance;<br><br>    fixture.detectChanges();<br><br>    return {bankAccountApiService, personApiService, component, fixture};<br>  }</pre><p>Inside the init function, we instantiate our mock services and invoke the setup function if provided. This ensures that the configureTestingModule is called with the correct mocked interfaces for each test case.</p><pre>{provide: PersonApiService, useValue: personApiService},</pre><p>Next we just setup our module with our providers. As we would do in the beforeEach() Method. Here it is important, we use ‘useValue’ instead of ‘useClass’, because we want to use our already instantiated and prepared mock service for the test-setup. At the end of the test-setup we can return the services, component and fixture and so on. So we can use it in our test for expectations if needed.</p><h3>Test Case Example</h3><p>Let’s take a look at a test case to illustrate how the init function enhances test setup.</p><pre>it(&#39;should return normal kid when person is under 18 and has less than 50k on the bank account&#39;, async () =&gt; {<br>  // arrange<br>  const { component } = await init(({ bankAccountApiService, personApiService }) =&gt; {<br>    // overwrite the mock services<br>    spyOn(bankAccountApiService, &#39;getBankAccount&#39;).and.returnValue(of({ balance: 20_000 }));<br>    spyOn(personApiService, &#39;getPerson&#39;).and.returnValue(of({ name: &#39;Alex&#39;, age: 17, hobby: &#39;soccer&#39; }));<br>  });<br><br>  // act<br>  const result = await firstValueFrom(component.richKid$);<br><br>  // assert<br>  expect(result).toEqual(&#39;Just a normal Kid :)&#39;);<br>});</pre><p>We have the typical structure of a testcase (arrange, act, assert). We call our init-function and we pass in the setup-function as parameter. In the implementation of the setup-function we override the methods of our services, so that the BankAccountApiServiceand PersonApiServicereturn the appropriate values.</p><p>If we run the test, we will see that everything works like a charm.</p><h3>Conclusion</h3><p>While this approach might seem complex initially, it offers a more elegant and maintainable solution compared to the traditional setup. Once implemented, it provides ease of extension and ensures a consistent testing environment across different scenarios.</p><p>For a comprehensive example, you can explore the <a href="https://stackblitz.com/~/github.com/Fab1anB/angular-component-testing">full code on StackBlitz</a>.</p><p>More Information about me at: <a href="https://www.fabi-solutions.dev/">https://www.fabi-solutions.dev/</a></p><p>Cheer. Clap &amp; Happy Coding! ❤</p><h3>Stackademic</h3><p><em>Thank you for reading until the end. Before you go:</em></p><ul><li><em>Please consider </em><strong><em>clapping</em></strong><em> and </em><strong><em>following</em></strong><em> the writer! 👏</em></li><li><em>Follow us on </em><a href="https://twitter.com/stackademichq"><strong><em>Twitter(X)</em></strong></a><em>, </em><a href="https://www.linkedin.com/company/stackademic"><strong><em>LinkedIn</em></strong></a><em>, and </em><a href="https://www.youtube.com/c/stackademic"><strong><em>YouTube</em></strong></a><strong><em>.</em></strong></li><li><em>Visit </em><a href="http://stackademic.com/"><strong><em>Stackademic.com</em></strong></a><em> to find out more about how we are democratizing free programming education around the world.</em></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bac41b7996a8" width="1" height="1" alt=""><hr><p><a href="https://blog.stackademic.com/optimizing-angular-test-setup-a-better-approach-than-beforeeach-bac41b7996a8">Optimizing Angular Test-Setup: A Better Approach Than beforeEach()</a> was originally published in <a href="https://blog.stackademic.com/">Stackademic</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Dependent Fields in Angular Reactive Forms]]></title>
            <link>https://medium.com/@Fabian_Birke/mastering-dependent-fields-in-angular-reactive-forms-c1e5ff8f9305?source=rss-59927c1fed9------2</link>
            <guid isPermaLink="false">https://medium.com/p/c1e5ff8f9305</guid>
            <category><![CDATA[forms]]></category>
            <category><![CDATA[reactive-forms]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Fabian Birke]]></dc:creator>
            <pubDate>Sat, 28 Oct 2023 08:00:57 GMT</pubDate>
            <atom:updated>2026-08-19T12:03:02.039Z</atom:updated>
            <content:encoded><![CDATA[<h4>Adding a new field that should be disabled under certain conditions sounds like a 10-minute task for a product manager or product owner, but why do many developers spend half a day on it?</h4><h3>Introduction</h3><p>Complex reactive forms in Angular can quickly become complicated.</p><p>In this tutorial, I will show you a way to make your Angular forms, even those with high dependencies, easily extensible and changeable. You can find the example at the end on <a href="https://angular-ivy-dy3h4g.stackblitz.io/">Stackblitz</a>.</p><figure><img alt="“Mastering complex reactive forms in Angular” by Fabian Birke" src="https://cdn-images-1.medium.com/max/1024/1*iABtNXUb6GmWj4pooxCirA.png" /></figure><h3>Getting Started</h3><p>Angular Component for the Form:</p><p>Let’s start with a simple example. We have a form with two fields, ipV4 and ipV6. When ipV4 contains text, ipV6 should be disabled, and vice versa. However, these fields are only displayed when the user is an admin. Let’s create a new Angular component example-form.component.ts to represent the form.</p><p>HTML in this component:</p><pre>&lt;form class=&quot;form-container&quot; [formGroup]=&quot;formGroup&quot;&gt;<br>  &lt;label&gt;IpV4&lt;/label&gt;<br>  &lt;input formControlName=&quot;ipV4&quot;&gt;<br>  &lt;label&gt;IpV6&lt;/label&gt;<br>  &lt;input formControlName=&quot;ipV6&quot;&gt;<br>&lt;/form&gt;</pre><p>TypeScript file for the component:</p><pre>export class ExampleFormComponent {<br><br>  public formGroup = new FormGroup({<br>    ipV4: new FormControl(&#39;&#39;),<br>    ipV6: new FormControl(&#39;&#39;),<br>  });<br>}</pre><p>Now we have a basic form, but without any dependencies between the fields.</p><p>Configuring Dependencies: Creating a example-form.config.ts File: To represent the dependencies between the form fields, we will create a new file named “form.config.ts,” which contains a configuration for each field.</p><p>First, let’s define two interfaces, FormModel and StateModel.</p><pre>// example-form.config.ts<br>export interface FormModel {<br>  ipV4: string | null;<br>  ipV6: string | null;<br>}</pre><pre>// example-form.config.ts<br>export interface StateModel extends FormModel {<br>  isAdmin: boolean;<br>}</pre><p>The FormModel interface corresponds to each field in the form, while the StateModel extends the form’s model to include external state, such as whether the user is an admin.</p><p>Next, let’s define what our fields can do, such as whether they can be disabled and whether they can be visible. We will define this in a separate interface.</p><pre>// example-form.config.ts<br>export interface ExampleFormConfigModel {<br>  isDisabled: (validationModel: StateModel) =&gt; boolean,<br>  isViewable: (validationModel: StateModel) =&gt; boolean,<br>}</pre><p>Both functions receive the StateModel as a parameter. This gives us enormous flexibility. We can react both to the global state from outside (e.g. isAdmin) and to the state of the fields in the form.</p><h3>Implement the configuration</h3><p>Now, we need to implement the configuration.</p><pre>// example-form.config.ts<br>type FormModelKeys = keyof FormModel;<br>export const formConfig: Record&lt;FormModelKeys, ExampleFormConfigModel&gt; = {<br>  ipV6: {<br>    isDisabled: (validationModel) =&gt; validationModel.ipV4 !== &#39;&#39;,<br>    isViewable: (validationModel) =&gt; validationModel.isAdmin<br>  },<br>  ipV4: {<br>    isDisabled: (validationModel) =&gt; validationModel.ipV6 !== &#39;&#39;,<br>    isViewable: (validationModel) =&gt; validationModel.isAdmin<br>  }<br>}</pre><p>The configuration is a dictionary. TypeScript provides the Recordtype for this purpose, where we have to specify a key and a value type. Since we need the configuration for each field in our form, we use keyof FormModel as the key and our FormConfigModelas the value.</p><p>Let’s take a look at the isDisabledfunction for ipV6. This function takes the validationModelas input and returns a boolean (as defined in the interface). It returns true when the ipV4property is filled. For ipV6, it’s the opposite. This clearly shows the dependency between the two fields. The isViewable function is structured similarly but depends on a state from outside the form.</p><h3>Using the Configuration:</h3><p>Let’s create an observable for our stateModel. This is necessary for using isViewable(). If you don’t need it, you can simply subscribe to the observable in the ngOnInit()lifecylcle hook. Our configuration file should be validated when the form changes. We also assume that isAdmin$ does not change.</p><pre>// example-form.component.ts<br>  public stateModel$: Observable&lt;StateModel&gt; = this.formGroup.valueChanges.pipe(<br>    startWith(null),<br>    withLatestFrom(this.isAdmin),<br>    map(<br>      ([_, isAdmin]) =&gt;<br>        ({ ...this.formGroup.getRawValue(), isAdmin } as StateModel)<br>    ),<br>    tap((stateModel) =&gt; {<br>      Object.entries(formConfig).map(([controlName, configModel]) =&gt; {<br>        if (configModel.isDisabled(stateModel)) {<br>          this.formGroup.get(controlName)?.disable({ emitEvent: false });<br>        } else {<br>          this.formGroup.get(controlName)?.enable({ emitEvent: false });<br>        }<br>      });<br><br>      this.formGroup.updateValueAndValidity({ emitEvent: false });<br>    })<br>  );</pre><p>First, we create our StateModelby copying all the necessary state into a common object. Then, we iterate over our configuration. The Object.entries()method provides us with the <strong>key</strong> and <strong>value</strong> of the dictionary, which we can use with the map function. On the configModel, we can now call our defined functions. We check whether changes in the form result in changes (disabling/enabling) of our fields.</p><p>Now, all that’s left is to add our isViewable()function to each field in the HTML as *ngIf.</p><pre>&lt;h1&gt;Dynamic Angular Forms&lt;/h1&gt;<br>&lt;form class=&quot;form-container&quot; [formGroup]=&quot;formGroup&quot; *ngIf=&quot;stateModel$ | async as stateModel&quot;&gt;<br>  &lt;ng-container *ngIf=&quot;formConfig.ipV4.isViewable(stateModel)&quot;&gt;<br>&lt;label&gt;IpV4&lt;/label&gt;<br>    &lt;input formControlName=&quot;ipV4&quot;&gt;<br>  &lt;/ng-container&gt;<br>  &lt;ng-container *ngIf=&quot;formConfig.ipV6.isViewable(stateModel)&quot;&gt;<br>    &lt;label&gt;IpV6&lt;/label&gt;<br>    &lt;input formControlName=&quot;ipV6&quot;&gt;<br>  &lt;/ng-container&gt;<br>&lt;/form&gt;</pre><p>Now, when the mocked value of isAdmin$=of(true); is set to false, the corresponding fields will be hidden.</p><h3>Conclusion</h3><p>This simple example demonstrates that dependencies within reactive forms can be made changeable, extensible, and, most importantly, understandable. While outsourcing the configuration to a separate file might seem complicated at first, this methodology has proven to be very effective in my projects.</p><p>More Information about me at: <a href="https://www.fabi-solutions.dev/">https://www.fabi-solutions.dev/</a></p><p><strong>See complete example on stackblitz</strong> <a href="https://angular-ivy-dy3h4g.stackblitz.io/">https://angular-ivy-dy3h4g.stackblitz.io</a></p><p>Feel free to connect with me. Clap.</p><p>❤ Happy Coding ❤.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c1e5ff8f9305" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular Tip 1 — filterNotNull() Operator]]></title>
            <link>https://medium.com/@Fabian_Birke/angular-tip-1-filternotnull-operator-c89739c64ab9?source=rss-59927c1fed9------2</link>
            <guid isPermaLink="false">https://medium.com/p/c89739c64ab9</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[rxjs]]></category>
            <dc:creator><![CDATA[Fabian Birke]]></dc:creator>
            <pubDate>Fri, 06 Oct 2023 07:14:00 GMT</pubDate>
            <atom:updated>2026-08-19T12:03:40.358Z</atom:updated>
            <content:encoded><![CDATA[<h3>Angular Tip 1 — filterNotNull() Operator</h3><h4>You will learn how to implement a first very useful custom RxJS-Operator which helps to handle null values in your RxJS stream.</h4><h3>Introduction</h3><p>RxJS remains the standard for reactive programming in Angular. Even though Signals are on the rise, the transition to “pure” Signals will still take years and may never be fully implemented in large projects. Whether you use Signals in conjunction with toObservable() or BehaviorSubjects, they are often initialized with null or undefined. When the Observable becomes a Hot Observable and receives a subscriber, the initial value (<strong>null</strong>/<strong>undefined</strong>) is emitted. This leads to endless null checks.</p><figure><img alt="“Filter null values in Angular observable streams with ease” by Fabian Birke" src="https://cdn-images-1.medium.com/max/1024/1*nd1lddkxWF3rOeoE9IBu0w.jpeg" /></figure><p>To simplify this we can write a custom operator to handle this problem.</p><pre>import {filter, Observable, OperatorFunction} from &#39;rxjs&#39;;<br><br>/**<br> * RxJS function which filters null or undefined value of a stream.<br> * e.g. you have a stream which emits null then this value is filtered.<br> * See spec file for more examples.<br> */<br>export const filterNotNull =<br> &lt;T&gt;(): OperatorFunction&lt;T, Exclude&lt;T, null | undefined&gt;&gt; =&gt;<br> (source$) =&gt;<br>  source$.pipe(filter((value) =&gt; value != null)) as Observable&lt;Exclude&lt;T, null | undefined&gt;&gt;;</pre><p>We create an operator called <strong>filterNotNull </strong>which allows only defined values and thus filters out the values <strong>null </strong>or <strong>undefined</strong>. This function can now be used in all Observable pipelines as follows.</p><pre>const potentialNullValue = null;<br><br>of(potentialNullValue).pipe(<br>filterNotNull(),<br>doSomething()<br>)</pre><p>Have Fun and happy Coding.</p><p>More Information about me at: <a href="https://www.fabi-solutions.dev/">https://www.fabi-solutions.dev/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c89739c64ab9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>