SlideShare a Scribd company logo
Angular components
unit testing
ngPoland 2018
Tomasz Borowski
• Frontend Developer at Jcommerce
• Trainer at Angular in Space
workshops
Angular components - unit testing
• why component is so special in testing?
• what testing tools do we get from Angular?
• how to deal with dependencies?
• example implementation of component tests
Why component is so special in testing?
Component
Class
Template is important part
of the component
• uses property bindings
• uses event bindings
• introduces dependencies
• contains logic
Component
Template
What testing tools do we get from Angular?
.createComponent(TestedComponent)
TestBed
.configureTestingModule({ imports: […], declarations: […], providers: […] })
ComponentFixture<TestedComponent>
.nativeElement.componentInstance .debugElement .detectChanges()
DebugElement
.triggerEventHandler(…)
.injector.get(…)
.nativeElement
.query(…) .queryAll(…)
elementinstance
How to deal with dependencies?
What can be a component dependency:
• input data
• injected services
• child components, directives, pipes
A
B
FC
E
D
In tests use only plain dependencies

and mock the complex ones.
Mock
B
Mock
C
How to mock dependencies?
class MockedService extends RealService {
constructor() { super(null); }
}
const mockedResource = {name: `John`};
spyOn(someService, `getResource`)
.and.returnValue(of(mockedResource));
@Component({
selector: `app-child`,
template: `mocked template`
})
class MockedComponent {
@Input() someInput;
@Output() someOutput;
constructor() { }
}
• allows to build, attack, repair
and destroy space ships
• accepts fleet name as input
Example: SpaceShips app SpaceShipService
LocalStorageService
SpaceFleetComponent
SpaceShipComponent
LongClickDirective source: http://spaceapp.angular-in-space.pl/
fleetName input
spaceShip input
Example: Preconditions
we execute CLI ng test command
we perform tests in browser
we use Jasmine & Karma
Let’s implement tests…
What’s next…
https://angular.io/guide/testing
Thank you for your attention!
Angular in Space

More Related Content

What's hot

1.microsoft visual studio 2010 test manager
1.microsoft visual studio 2010  test manager1.microsoft visual studio 2010  test manager
1.microsoft visual studio 2010 test manager
Ashwin Jujgar
 

What's hot (20)

1.microsoft visual studio 2010 test manager
1.microsoft visual studio 2010  test manager1.microsoft visual studio 2010  test manager
1.microsoft visual studio 2010 test manager
 
Tdd for php
Tdd for phpTdd for php
Tdd for php
 
2 selenium-aakar gupte
2 selenium-aakar gupte2 selenium-aakar gupte
2 selenium-aakar gupte
 
Unit tests and TDD
Unit tests and TDDUnit tests and TDD
Unit tests and TDD
 
RFT Tutorial - 9 How To Create A Properties Verification Point In Rft For Tes...
RFT Tutorial - 9 How To Create A Properties Verification Point In Rft For Tes...RFT Tutorial - 9 How To Create A Properties Verification Point In Rft For Tes...
RFT Tutorial - 9 How To Create A Properties Verification Point In Rft For Tes...
 
Unit test documentation
Unit test documentationUnit test documentation
Unit test documentation
 
Automation using ibm rft
Automation using ibm rftAutomation using ibm rft
Automation using ibm rft
 
Unit Tesing in iOS
Unit Tesing in iOSUnit Tesing in iOS
Unit Tesing in iOS
 
How to kill test flake in appium
How to kill test flake in appiumHow to kill test flake in appium
How to kill test flake in appium
 
Zero to hero in agile automation testing
Zero to hero in agile automation testingZero to hero in agile automation testing
Zero to hero in agile automation testing
 
RFT Tutorial 4 How Do We Record A Script Using Rational Functional Tester - RFT
RFT Tutorial 4 How Do We Record A Script Using Rational Functional Tester - RFTRFT Tutorial 4 How Do We Record A Script Using Rational Functional Tester - RFT
RFT Tutorial 4 How Do We Record A Script Using Rational Functional Tester - RFT
 
How to be an awesome test automation professional
How to be an awesome test automation professionalHow to be an awesome test automation professional
How to be an awesome test automation professional
 
Automation is Easy! (python version)
Automation is Easy! (python version)Automation is Easy! (python version)
Automation is Easy! (python version)
 
Automated Software Testing
Automated Software TestingAutomated Software Testing
Automated Software Testing
 
Testing on frontend
Testing on frontendTesting on frontend
Testing on frontend
 
Automated tests to a REST API
Automated tests to a REST APIAutomated tests to a REST API
Automated tests to a REST API
 
Contract testing: Beyond API functional testing
Contract testing: Beyond API functional testingContract testing: Beyond API functional testing
Contract testing: Beyond API functional testing
 
Rft courseware
Rft coursewareRft courseware
Rft courseware
 
Kristian Karl - Experiences of Test Automation at Spotify - EuroSTAR 2013
Kristian Karl - Experiences of Test Automation at Spotify - EuroSTAR 2013Kristian Karl - Experiences of Test Automation at Spotify - EuroSTAR 2013
Kristian Karl - Experiences of Test Automation at Spotify - EuroSTAR 2013
 
Angular genericforms2
Angular genericforms2Angular genericforms2
Angular genericforms2
 

Similar to Angular components-unit-testing

Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Codecamp Romania
 

Similar to Angular components-unit-testing (20)

Testing Angular
Testing AngularTesting Angular
Testing Angular
 
Angular Unit Testing
Angular Unit TestingAngular Unit Testing
Angular Unit Testing
 
Angular js 2
Angular js 2Angular js 2
Angular js 2
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
 
Angular IO
Angular IOAngular IO
Angular IO
 
Performance Testing in Continous Delivery
Performance Testing in Continous DeliveryPerformance Testing in Continous Delivery
Performance Testing in Continous Delivery
 
Blazor Stability Testing Tools for Bullet Proof Applications
Blazor Stability Testing Tools for Bullet Proof ApplicationsBlazor Stability Testing Tools for Bullet Proof Applications
Blazor Stability Testing Tools for Bullet Proof Applications
 
Devday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvuDevday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvu
 
Unit Testing in Angular
Unit Testing in AngularUnit Testing in Angular
Unit Testing in Angular
 
Angular Application Testing
Angular Application TestingAngular Application Testing
Angular Application Testing
 
Angular2 with type script
Angular2 with type scriptAngular2 with type script
Angular2 with type script
 
02 - Angular Structural Elements - 1.pdf
02 - Angular Structural Elements - 1.pdf02 - Angular Structural Elements - 1.pdf
02 - Angular Structural Elements - 1.pdf
 
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
 
JavaScript Unit Testing
JavaScript Unit TestingJavaScript Unit Testing
JavaScript Unit Testing
 
Reliable mobile test automation
Reliable mobile test automationReliable mobile test automation
Reliable mobile test automation
 
Angular
AngularAngular
Angular
 
TechEvent 2019: Nachhaltige Client-Architekturen mit Angular Elements; Thomas...
TechEvent 2019: Nachhaltige Client-Architekturen mit Angular Elements; Thomas...TechEvent 2019: Nachhaltige Client-Architekturen mit Angular Elements; Thomas...
TechEvent 2019: Nachhaltige Client-Architekturen mit Angular Elements; Thomas...
 
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
 
Component Testing Slides.pptx
Component Testing Slides.pptxComponent Testing Slides.pptx
Component Testing Slides.pptx
 
Level Up Your Salesforce Unit Testing
Level Up Your Salesforce Unit TestingLevel Up Your Salesforce Unit Testing
Level Up Your Salesforce Unit Testing
 

More from Tomasz Borowski

Ionic framework - aplikacja mobilna w 15 minut
Ionic framework - aplikacja mobilna w 15 minutIonic framework - aplikacja mobilna w 15 minut
Ionic framework - aplikacja mobilna w 15 minut
Tomasz Borowski
 
Rozszerzona rzeczywistość - nowe spojrzenie na świat
Rozszerzona rzeczywistość - nowe spojrzenie na światRozszerzona rzeczywistość - nowe spojrzenie na świat
Rozszerzona rzeczywistość - nowe spojrzenie na świat
Tomasz Borowski
 

More from Tomasz Borowski (12)

Tworzenie niezależnego środowiska do developmentu aplikacji frontendowej
Tworzenie niezależnego środowiska do developmentu aplikacji frontendowejTworzenie niezależnego środowiska do developmentu aplikacji frontendowej
Tworzenie niezależnego środowiska do developmentu aplikacji frontendowej
 
Ionic framework - aplikacja mobilna w 15 minut
Ionic framework - aplikacja mobilna w 15 minutIonic framework - aplikacja mobilna w 15 minut
Ionic framework - aplikacja mobilna w 15 minut
 
Aplikacje mobilne tworzone w technologiach webowych
Aplikacje mobilne tworzone w technologiach webowychAplikacje mobilne tworzone w technologiach webowych
Aplikacje mobilne tworzone w technologiach webowych
 
Paleta możliwości web developera
Paleta możliwości web developeraPaleta możliwości web developera
Paleta możliwości web developera
 
Grywalizacja rzeczywistości, czyli jak uciec od kompa
Grywalizacja rzeczywistości, czyli jak uciec od kompaGrywalizacja rzeczywistości, czyli jak uciec od kompa
Grywalizacja rzeczywistości, czyli jak uciec od kompa
 
Produkcja aplikacji internetowych
Produkcja aplikacji internetowychProdukcja aplikacji internetowych
Produkcja aplikacji internetowych
 
How I Became a Spaceship Commander
How I Became a Spaceship CommanderHow I Became a Spaceship Commander
How I Became a Spaceship Commander
 
Grywalizacja w trosce o zdrowie
Grywalizacja w trosce o zdrowieGrywalizacja w trosce o zdrowie
Grywalizacja w trosce o zdrowie
 
Haxorz day 7
Haxorz day 7Haxorz day 7
Haxorz day 7
 
7 rzeczy, które zespoły IT mogą nauczyć się ze sportów drużynowych
7 rzeczy, które zespoły IT mogą nauczyć się ze sportów drużynowych7 rzeczy, które zespoły IT mogą nauczyć się ze sportów drużynowych
7 rzeczy, które zespoły IT mogą nauczyć się ze sportów drużynowych
 
Rozszerzona rzeczywistość - nowe spojrzenie na świat
Rozszerzona rzeczywistość - nowe spojrzenie na światRozszerzona rzeczywistość - nowe spojrzenie na świat
Rozszerzona rzeczywistość - nowe spojrzenie na świat
 
Psychologia w grach
Psychologia w grachPsychologia w grach
Psychologia w grach
 

Recently uploaded

Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 

Recently uploaded (20)

Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
 
Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Kraków
 

Angular components-unit-testing

  • 2. Tomasz Borowski • Frontend Developer at Jcommerce • Trainer at Angular in Space workshops
  • 3. Angular components - unit testing • why component is so special in testing? • what testing tools do we get from Angular? • how to deal with dependencies? • example implementation of component tests
  • 4. Why component is so special in testing? Component Class Template is important part of the component • uses property bindings • uses event bindings • introduces dependencies • contains logic Component Template
  • 5. What testing tools do we get from Angular? .createComponent(TestedComponent) TestBed .configureTestingModule({ imports: […], declarations: […], providers: […] }) ComponentFixture<TestedComponent> .nativeElement.componentInstance .debugElement .detectChanges() DebugElement .triggerEventHandler(…) .injector.get(…) .nativeElement .query(…) .queryAll(…) elementinstance
  • 6. How to deal with dependencies? What can be a component dependency: • input data • injected services • child components, directives, pipes A B FC E D In tests use only plain dependencies
 and mock the complex ones. Mock B Mock C
  • 7. How to mock dependencies? class MockedService extends RealService { constructor() { super(null); } } const mockedResource = {name: `John`}; spyOn(someService, `getResource`) .and.returnValue(of(mockedResource)); @Component({ selector: `app-child`, template: `mocked template` }) class MockedComponent { @Input() someInput; @Output() someOutput; constructor() { } }
  • 8. • allows to build, attack, repair and destroy space ships • accepts fleet name as input Example: SpaceShips app SpaceShipService LocalStorageService SpaceFleetComponent SpaceShipComponent LongClickDirective source: http://spaceapp.angular-in-space.pl/ fleetName input spaceShip input
  • 9. Example: Preconditions we execute CLI ng test command we perform tests in browser we use Jasmine & Karma
  • 12. Thank you for your attention! Angular in Space