Posts

Showing posts with the label JavaScript

Jasmine/Karma- Writing Unit Test Cases for Angular

Image
What is Jasmine?   DOM-less simple JavaScript testing framework. Jasmine is a Behavior Driven Development testing framework for JavaScript. It does not rely on browsers, DOM, or any JavaScript framework. Thus, it's suited for websites, Node.js projects, or anywhere that JavaScript can run. What is Karma?   Spectacular Test Runner for JavaScript. Karma is not a testing framework, nor an assertion library. Karma just launches a HTTP server, and generates the test runner HTML file you probably already know from your favorite testing framework. So for testing purposes you can use pretty much anything you like In short, Jasmine: Javascript Testing Framework.   Can be used as TDD Karma: Browser testing. Used as a Rest Runner TESTING ANGULAR COMPONENTS Consider the below angular component: app.component.ts app.component.spec.ts TESTING SERVICES Angular services contain UI-independent reusable business logic of the application. myservice.service.ts The spec file of this...

Functional Programming in JavaScript

Image
There are different "paradigms" (just a fancy word which means "ways of programming"!) in JS programming like Object Oriented, Functional, Imperative etc Object Oriented involves all the OOPS concepts as we all know like Inheritance, Abstraction, Encapsulation, Polymorphism Imperative programming involves coding in an imperative way, l ike first do this, then do that etc Functional Programming Everything is expressed in the program in terms of “functions”. This function takes an input and returns the transformed data as output   Avoid side effects in functions, Use PURE functions: Any function that computes its output purely from the inputs it receives as arguments (should not modify other global data, change any other variable). Printing something to the console is also not “returning” an output. Basically, the function has to read the input, take that, and only that to compute an output. Functional programming means thinking of functions as purely as poss...

RxJS

Image
 RxJS: Reactive Extensions for JavaScript We need RxJS to handle asynchronous data easily  Asynchronous data: Data from HTTP response, data from a port, user click events, timer etc Observer and Observable Observer: RxJS object that emits data stream Observable: Entity that  is listening to the stream In order to initiate communication between data and listener in RxJS scope, first they need to be converted to "observer" and "observable" Observer subscribes to the observable to listen to the data stream Use of "subscribe:" Used to listen to the data stream OPERATORS Operators are small pieces of code that can be applied as pre-processing logic before data comes to the listener Summary of common operators: map:   used to manipulate responses from the API filter: used to filter values based on condition. Same as ES6 filter but this is for Observable merge:  This operator combines a number of observables streams and concurrently emits all values from ever...

JavaScript Buzzwords: Closures, Hoisting, Currying, Event Loop, Bind/Call/Apply

Image
1.     Closures JS by itself is not a language that is OOPS friendly. In order to mock the Abstraction/Encapsulation in the traditional OOPS, closures are used. A closure is just a pattern of writing a function (here function is treated as a “class”) Without Closures: No matter how many times you call CounterTest(), the  count  value is always “generated” fresh and hence  count   always increments from 0 and returns 1. But, what if you had to preserve the count value and on each call, it had to increment based on the previous result. Ex:  Closure to the rescue!   With Closures: Summarizing: ·          Child function has access to parent function’s private variables. ·          Parent function returns the child function and only exposes this to the outside world (Abstraction/Encapsulation) ·          Outside functions ar...