<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Syed Tabarak Ulla</title>
    <description>The latest articles on DEV Community by Syed Tabarak Ulla (@stabarak).</description>
    <link>https://dev.to/stabarak</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1043376%2Fb359d6ca-aeb9-4608-84dc-482281325da2.png</url>
      <title>DEV Community: Syed Tabarak Ulla</title>
      <link>https://dev.to/stabarak</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/stabarak"/>
    <language>en</language>
    <item>
      <title>Adding NgRx to the Angular Application</title>
      <dc:creator>Syed Tabarak Ulla</dc:creator>
      <pubDate>Sat, 15 Apr 2023 17:11:28 +0000</pubDate>
      <link>https://dev.to/stabarak/adding-ngrx-to-the-angular-application-5c8h</link>
      <guid>https://dev.to/stabarak/adding-ngrx-to-the-angular-application-5c8h</guid>
      <description>&lt;p&gt;Greetings to everyone!&lt;/p&gt;

&lt;p&gt;In this blog I'm going to show you the steps you'll have to follow integrate the &lt;strong&gt;NgRx&lt;/strong&gt; into your &lt;strong&gt;Angular&lt;/strong&gt; application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fts0ee6x6b919amtyrcdy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fts0ee6x6b919amtyrcdy.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step-1: Install the &lt;strong&gt;@ngrx/store&lt;/strong&gt; package into the application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @ngrx/store
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step-2: To work with the redux dev toolkit will have to install the &lt;strong&gt;@ngrx/store-devtools&lt;/strong&gt; package.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @ngrx/store-devtools 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Stpe-3: Now let's modify the &lt;strong&gt;app.module.ts&lt;/strong&gt; file to link the redux store and the redux dev tool.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { StoreModule } from '@ngrx/store';
import { StoreDevtoolsModule } from '@ngrx/store-devtools'; 

@NgModule({ 
  imports: [
    StoreModule.forRoot({ }),
    StoreDevtoolsModule.instrument({
      maxAge: 25,
      autoPause: true,
      trace: false,
      traceLimit: 25,
    })
  ]
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step-4: Now create &lt;strong&gt;store&lt;/strong&gt; folder for your state management under the &lt;strong&gt;app&lt;/strong&gt; folder. And then under this create these below files&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;action.ts&lt;/li&gt;
&lt;li&gt;reducer.ts&lt;/li&gt;
&lt;li&gt;selector.ts&lt;/li&gt;
&lt;li&gt;state.model.ts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Step-5: Inside the &lt;strong&gt;action.ts&lt;/strong&gt; create actions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createAction } from '@ngrx/store';

export const SampleAction = createAction('[Sample] Message');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step-6: In the &lt;strong&gt;reducer.ts&lt;/strong&gt; create reducer to update the state whenever the actions get dispatched.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { MockInterface } from './model';
import * as AppActions from './action';
import { createReducer, on } from '@ngrx/store';

interface InitialState = {
  message: string;
};

const initialState: InitialState = {
  message: '',
};

export const appReducer = createReducer(
  initialState,
  on(AppActions.SampleAction, (state) =&amp;gt; ({
    ...state,
    message: 'Hello World!',
  }))
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step-7: Will add a interface for our state in the &lt;strong&gt;state.model.ts&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { InitialState } from './model';

export interface AppState {
  readonly app: InitialState;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step-8: Let's add selectors in the &lt;strong&gt;selector.ts&lt;/strong&gt; to fetch the store value&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { AppState } from './state.model';

export const selectApp = (state: AppState) =&amp;gt; state.app;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step-9: Now in the &lt;strong&gt;app.module.ts&lt;/strong&gt; file where we have imported the StoreModule, will need to add the reducer there.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  StoreModule.forRoot({
    app: appReducer,
  }),
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step-9: Let's dispatch the action to update state of the store with the empty message to have some message. &lt;/p&gt;

&lt;p&gt;In your &lt;strong&gt;app.component.ts&lt;/strong&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component, OnInit } from '@angular/core';

import { Store } from '@ngrx/store';
import { AppState } from '../store/state.model';
import { SampleAction } from 'src/app/store/action';

@Component({ 
  selector: 'app-root',
  templateUrl: './app.component.html',
})
export class AppComponent implements OnInit {
  constructor(private store: Store&amp;lt;AppState&amp;gt;) {}

  ngOnInit(): void {
    this.store.dispatch(SampleAction());
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step-10: After successfully dispatching the action to update message in the store, now will fetch the store to read the message through the help of the &lt;strong&gt;Selector&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Add new variable in the &lt;strong&gt;app.component.ts&lt;/strong&gt; and then update the &lt;strong&gt;ngOnInit&lt;/strong&gt; function to read the message from the store.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class AppComponent implements OnInit {
  message: string;

  constructor(private store: Store&amp;lt;AppState&amp;gt;) {}

  ngOnInit(): void {
    this.store.dispatch(SampleAction());
    this.store.pipe(select('app')).subscribe((state) =&amp;gt; {
      this.message = state.message;
    });
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step-11: In the &lt;strong&gt;app.component.html&lt;/strong&gt; let's bind the &lt;strong&gt;message&lt;/strong&gt; variable to display the message we have read from the store.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;h1&amp;gt;{{ message }}&amp;lt;/h1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let me know in the comments if there's any issues/doubts with this article, and if there's any suggestions for me please comment down. Thank you!.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>ngrx</category>
      <category>redux</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Create Angular project with Jest Testing Framework</title>
      <dc:creator>Syed Tabarak Ulla</dc:creator>
      <pubDate>Sat, 11 Mar 2023 22:23:46 +0000</pubDate>
      <link>https://dev.to/stabarak/create-angular-project-with-jest-testing-framework-2kl8</link>
      <guid>https://dev.to/stabarak/create-angular-project-with-jest-testing-framework-2kl8</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F58t5vioiecjc45zzc3d4.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F58t5vioiecjc45zzc3d4.jpeg" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
STEP:1&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -g @angular/cli
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;STEP:2&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ng new my-app-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;STEP:3&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd my-app-name
ng serve --open
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;STEP:4&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install jest @types/jest jest-preset-angular --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;STEP:5&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm uninstall karma karma-chrome-launcher karma-coverage-istanbul-reporter karma-jasmine karma-jasmine-html-reporter @types/jasmine @types/jasminewd2 jasmine-core jasmine-spec-reporter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the above code didn't removed the packages related to &lt;strong&gt;Karma&lt;/strong&gt; and &lt;strong&gt;Jasmine&lt;/strong&gt;, you can remove it manually from the &lt;strong&gt;package.json&lt;/strong&gt; file. And then run the &lt;code&gt;npm install&lt;/code&gt; to update the &lt;strong&gt;package-lock.json&lt;/strong&gt; file. &lt;/p&gt;

&lt;p&gt;STEP:6&lt;br&gt;
Remove &lt;strong&gt;test&lt;/strong&gt; from the &lt;strong&gt;angular.json&lt;/strong&gt; file&lt;/p&gt;

&lt;p&gt;STEP:7&lt;br&gt;
Replace &lt;strong&gt;test&lt;/strong&gt; with &lt;strong&gt;jest&lt;/strong&gt; on the scripts in &lt;strong&gt;package.json&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;"test": "jest",&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;STEP:8&lt;br&gt;
Create new file as &lt;strong&gt;setupJest.ts&lt;/strong&gt; on root level and add below code in it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'jest-preset-angular/setup-jest';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;STEP:9&lt;br&gt;
Remove if these files exist in the project&lt;br&gt;
&lt;strong&gt;karma.conf.js&lt;/strong&gt; and &lt;strong&gt;test.ts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;STEP:10&lt;br&gt;
Update the &lt;strong&gt;types&lt;/strong&gt; value in the &lt;strong&gt;tsconfig.spec.json&lt;/strong&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"types": [
      "jest",
      "node"
    ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;STEP:11&lt;br&gt;
Add below code on the &lt;strong&gt;package.json&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"jest": {
    "preset": "jest-preset-angular",
    "setupFilesAfterEnv": [
      "&amp;lt;rootDir&amp;gt;/setupJest.ts"
    ],
    "testPathIgnorePatterns": [
      "&amp;lt;rootDir&amp;gt;/node_modules/",
      "&amp;lt;rootDir&amp;gt;/dist/"
    ],
    "globals": {
      "ts-jest": {
        "tsconfig": "&amp;lt;rootDir&amp;gt;/tsconfig.spec.json",
        "stringifyContentPathRegex": "\\.html$"
      }
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally now we can test the application by running the below command on terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run test
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you need boilerplate code for Angular app with Jest test framework, please checkout my &lt;strong&gt;GitHub&lt;/strong&gt; repo &lt;a href="https://github.com/Stabarak/Angular-with-Jest.git"&gt;Angular-with-Jest&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>jest</category>
      <category>testing</category>
      <category>framework</category>
    </item>
  </channel>
</rss>
