SlideShare a Scribd company logo
TDD in AngularJS
Andrey Alpert


Developer with 9 years experience



Working for 2 years as Frontend developer



Using Angular in commercial projects for more
than a year
Presentation aalpert v6а+++
Presentation aalpert v6а+++
Presentation aalpert v6а+++


It is all about not mixing concerns



Follow the guidelines



If it is hard to make a test, probably your code is

wrong
function PasswordCtrl() {
var msg = $('.ex1 span');
var input = $('.ex1 input');
var strength;
this.grade = function() {
msg.removeClass(strength);
var pwd = input.val();
password.text(pwd);
if (pwd.length > 8) {
strength = 'strong';
} else if (pwd.length > 3) {
strength = 'medium';
} else {
strength = 'weak';
}
msg
.addClass(strength)
.text(strength);
}}
function PasswordCtrl() {
var msg = $('.ex1 span');
var input = $('.ex1 input');
var strength;
this.grade = function() {
msg.removeClass(strength);
var pwd = input.val();
password.text(pwd);
if (pwd.length > 8) {
strength = 'strong';
} else if (pwd.length > 3) {
strength = 'medium';
} else {
strength = 'weak';
}
msg
.addClass(strength)
.text(strength);
}}

var input = $('<input type="text"/>');
var span = $('<span>');
$('body').html('<div class="ex1">')
.find('div')
.append(input)
.append(span);
var pc = new PasswordCtrl();
input.val('abc');
pc.grade();
expect(span.text()).toEqual('weak');
$('body').html('');
function PasswordCtrl() {
var msg = $('.ex1 span');
var input = $('.ex1 input');
var strength;
this.grade = function() {
msg.removeClass(strength);
var pwd = input.val();
password.text(pwd);
if (pwd.length > 8) {
strength = 'strong';
} else if (pwd.length > 3) {
strength = 'medium';
} else {
strength = 'weak';
}
msg
.addClass(strength)
.text(strength);
}}

var input = $('<input type="text"/>');
var span = $('<span>');
$('body').html('<div class="ex1">')
.find('div')
.append(input)
.append(span);
var pc = new PasswordCtrl();
input.val('abc');
pc.grade();
expect(span.text()).toEqual('weak');
$('body').html('');
function PasswordCtrl($scope) {
$scope.password = '';
$scope.grade = function() {
var size = $scope.password.length;
if (size > 8) {
$scope.strength = 'strong';
} else if (size > 3) {
$scope.strength = 'medium';
} else {
$scope.strength = 'weak';
}
};}
var $scope = {};var pc = $controller('PasswordCtrl', {
$scope: $scope });
$scope.password = 'abc';
$scope.grade();
expect($scope.strength).toEqual('weak');


Directive is a unique and powerful feature, available
only in Angular



Directives let you invent new HTML tags



They can add:


Behaviors



Data Binding



Replace or extend HTML elements

<div ng-controller="AlertDemoCtrl">
<alert ng-repeat="alert in alerts" type="alert.type" close="closeAlert($index)">
{{alert.msg}}
</alert>
<button class='btn' ng-click="addAlert()">Add Alert</button></div>
app.directive('greeting', function () {
return {
restrict: 'E',
replace: true,
scope: {
'grName': '@'
}
template: '<div>hi there, {{grName}}</div>'
};});
describe('Test directive', function() {
var $compile;
var $rootScope;
beforeEach(module('myApp'));

beforeEach(inject(function(_$compile_, _$rootScope_){
$compile = _$compile_;
$rootScope = _$rootScope_;
}));
it('Replaces the element', function() {
var element = $compile('<greeting gr-name="Andrey"></greeting>')($rootScope);
$rootScope.$digest();
expect(element.html()).toContain("hi there, Andrey");
});});
Presentation aalpert v6а+++
Presentation aalpert v6а+++
npm install -g karma
karma init
karma start
karma run
Presentation aalpert v6а+++
Presentation aalpert v6а+++
Thanks for your time

and Happy testing

More Related Content

What's hot (7)

PPT
Mocking Dependencies in PHPUnit
mfrost503
 
PPTX
Conditional Statementfinal PHP 02
mohamedsaad24
 
PDF
RSpec matchers
rupicon
 
PPT
Test driven development_for_php
Lean Teams Consultancy
 
PPTX
Xenogenetics for PL/SQL - infusing with Java best practices
Lucas Jellema
 
PPT
JavaServer Faces Anti-Patterns and Pitfalls
Dennis Byrne
 
ODP
Modernising Legacy Code
SamThePHPDev
 
Mocking Dependencies in PHPUnit
mfrost503
 
Conditional Statementfinal PHP 02
mohamedsaad24
 
RSpec matchers
rupicon
 
Test driven development_for_php
Lean Teams Consultancy
 
Xenogenetics for PL/SQL - infusing with Java best practices
Lucas Jellema
 
JavaServer Faces Anti-Patterns and Pitfalls
Dennis Byrne
 
Modernising Legacy Code
SamThePHPDev
 

Similar to Presentation aalpert v6а+++ (20)

PDF
Alexey Tsoy Meta Programming in C++ 16.11.17
LogeekNightUkraine
 
PDF
Security Challenges in Node.js
Websecurify
 
PDF
Einführung in TypeScript
Demian Holderegger
 
PDF
Test driven node.js
Jay Harris
 
PPTX
Javascripting.pptx
Vinod Srivastava
 
PDF
Testing most things in JavaScript - LeedsJS 31/05/2017
Colin Oakley
 
PDF
Automation in angular js
Marcin Wosinek
 
PDF
JavaScript - Chapter 4 - Types and Statements
WebStackAcademy
 
PDF
ESNext for humans - LvivJS 16 August 2014
Jan Jongboom
 
PDF
Angular js 24 april 2013 amsterdamjs
Marcin Wosinek
 
PDF
Pragmatic Real-World Scala
parag978978
 
PDF
Pragmatic Real-World Scala (short version)
Jonas Bonér
 
PDF
Writing testable js [by Ted Piotrowski]
JavaScript Meetup HCMC
 
PDF
How to write code you won't hate tomorrow
Pete McFarlane
 
PPTX
AST - the only true tool for building JavaScript
Ingvar Stepanyan
 
PDF
TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Okuno Kentaro
 
PDF
java-programming.pdf
Prof. Dr. K. Adisesha
 
ODP
Parsing with Perl6 Grammars
abrummett
 
PPTX
yash shakya.pptx
yashshakya13
 
PDF
The Joy of Smartmatch
Andrew Shitov
 
Alexey Tsoy Meta Programming in C++ 16.11.17
LogeekNightUkraine
 
Security Challenges in Node.js
Websecurify
 
Einführung in TypeScript
Demian Holderegger
 
Test driven node.js
Jay Harris
 
Javascripting.pptx
Vinod Srivastava
 
Testing most things in JavaScript - LeedsJS 31/05/2017
Colin Oakley
 
Automation in angular js
Marcin Wosinek
 
JavaScript - Chapter 4 - Types and Statements
WebStackAcademy
 
ESNext for humans - LvivJS 16 August 2014
Jan Jongboom
 
Angular js 24 april 2013 amsterdamjs
Marcin Wosinek
 
Pragmatic Real-World Scala
parag978978
 
Pragmatic Real-World Scala (short version)
Jonas Bonér
 
Writing testable js [by Ted Piotrowski]
JavaScript Meetup HCMC
 
How to write code you won't hate tomorrow
Pete McFarlane
 
AST - the only true tool for building JavaScript
Ingvar Stepanyan
 
TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Okuno Kentaro
 
java-programming.pdf
Prof. Dr. K. Adisesha
 
Parsing with Perl6 Grammars
abrummett
 
yash shakya.pptx
yashshakya13
 
The Joy of Smartmatch
Andrew Shitov
 
Ad

Recently uploaded (20)

PDF
The Past, Present & Future of Kenya's Digital Transformation
Moses Kemibaro
 
PDF
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PDF
OpenInfra ID 2025 - Are Containers Dying? Rethinking Isolation with MicroVMs.pdf
Muhammad Yuga Nugraha
 
PPTX
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PDF
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PPTX
The Yotta x CloudStack Advantage: Scalable, India-First Cloud
ShapeBlue
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
PPTX
Machine Learning Benefits Across Industries
SynapseIndia
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
How Current Advanced Cyber Threats Transform Business Operation
Eryk Budi Pratama
 
PDF
Per Axbom: The spectacular lies of maps
Nexer Digital
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
PDF
visibel.ai Company Profile – Real-Time AI Solution for CCTV
visibelaiproject
 
PPTX
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
PDF
SalesForce Managed Services Benefits (1).pdf
TechForce Services
 
The Past, Present & Future of Kenya's Digital Transformation
Moses Kemibaro
 
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
OpenInfra ID 2025 - Are Containers Dying? Rethinking Isolation with MicroVMs.pdf
Muhammad Yuga Nugraha
 
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
 
The Future of Artificial Intelligence (AI)
Mukul
 
The Yotta x CloudStack Advantage: Scalable, India-First Cloud
ShapeBlue
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
Machine Learning Benefits Across Industries
SynapseIndia
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
How Current Advanced Cyber Threats Transform Business Operation
Eryk Budi Pratama
 
Per Axbom: The spectacular lies of maps
Nexer Digital
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
visibel.ai Company Profile – Real-Time AI Solution for CCTV
visibelaiproject
 
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
SalesForce Managed Services Benefits (1).pdf
TechForce Services
 
Ad

Presentation aalpert v6а+++

  • 2.  Developer with 9 years experience  Working for 2 years as Frontend developer  Using Angular in commercial projects for more than a year
  • 6.  It is all about not mixing concerns  Follow the guidelines  If it is hard to make a test, probably your code is wrong
  • 7. function PasswordCtrl() { var msg = $('.ex1 span'); var input = $('.ex1 input'); var strength; this.grade = function() { msg.removeClass(strength); var pwd = input.val(); password.text(pwd); if (pwd.length > 8) { strength = 'strong'; } else if (pwd.length > 3) { strength = 'medium'; } else { strength = 'weak'; } msg .addClass(strength) .text(strength); }}
  • 8. function PasswordCtrl() { var msg = $('.ex1 span'); var input = $('.ex1 input'); var strength; this.grade = function() { msg.removeClass(strength); var pwd = input.val(); password.text(pwd); if (pwd.length > 8) { strength = 'strong'; } else if (pwd.length > 3) { strength = 'medium'; } else { strength = 'weak'; } msg .addClass(strength) .text(strength); }} var input = $('<input type="text"/>'); var span = $('<span>'); $('body').html('<div class="ex1">') .find('div') .append(input) .append(span); var pc = new PasswordCtrl(); input.val('abc'); pc.grade(); expect(span.text()).toEqual('weak'); $('body').html('');
  • 9. function PasswordCtrl() { var msg = $('.ex1 span'); var input = $('.ex1 input'); var strength; this.grade = function() { msg.removeClass(strength); var pwd = input.val(); password.text(pwd); if (pwd.length > 8) { strength = 'strong'; } else if (pwd.length > 3) { strength = 'medium'; } else { strength = 'weak'; } msg .addClass(strength) .text(strength); }} var input = $('<input type="text"/>'); var span = $('<span>'); $('body').html('<div class="ex1">') .find('div') .append(input) .append(span); var pc = new PasswordCtrl(); input.val('abc'); pc.grade(); expect(span.text()).toEqual('weak'); $('body').html('');
  • 10. function PasswordCtrl($scope) { $scope.password = ''; $scope.grade = function() { var size = $scope.password.length; if (size > 8) { $scope.strength = 'strong'; } else if (size > 3) { $scope.strength = 'medium'; } else { $scope.strength = 'weak'; } };} var $scope = {};var pc = $controller('PasswordCtrl', { $scope: $scope }); $scope.password = 'abc'; $scope.grade(); expect($scope.strength).toEqual('weak');
  • 11.  Directive is a unique and powerful feature, available only in Angular  Directives let you invent new HTML tags  They can add:  Behaviors  Data Binding  Replace or extend HTML elements <div ng-controller="AlertDemoCtrl"> <alert ng-repeat="alert in alerts" type="alert.type" close="closeAlert($index)"> {{alert.msg}} </alert> <button class='btn' ng-click="addAlert()">Add Alert</button></div>
  • 12. app.directive('greeting', function () { return { restrict: 'E', replace: true, scope: { 'grName': '@' } template: '<div>hi there, {{grName}}</div>' };});
  • 13. describe('Test directive', function() { var $compile; var $rootScope; beforeEach(module('myApp')); beforeEach(inject(function(_$compile_, _$rootScope_){ $compile = _$compile_; $rootScope = _$rootScope_; })); it('Replaces the element', function() { var element = $compile('<greeting gr-name="Andrey"></greeting>')($rootScope); $rootScope.$digest(); expect(element.html()).toContain("hi there, Andrey"); });});
  • 16. npm install -g karma karma init karma start karma run
  • 19. Thanks for your time and Happy testing