{"id":3862,"date":"2020-07-28T14:09:12","date_gmt":"2020-07-28T14:09:12","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=3862"},"modified":"2020-08-05T15:26:10","modified_gmt":"2020-08-05T15:26:10","slug":"how-to-test-typescript-methods-using-spyon","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/how-to-test-typescript-methods-using-spyon\/","title":{"rendered":"Basic of Testing &#8211; Jasmine"},"content":{"rendered":"<p><strong>Before start testing method in components we initialize few things as below<\/strong><\/p>\n<ol>\n<li>beforeEach method helps in initializing the values needed for testing<\/li>\n<li>describe are similar to JUnit class and it are similar to junit class methods which tests particular method in src code<\/li>\n<li>TestBed is needed while testing both component and html.You need to configure the TestBed before each test, adding any components, modules and services you need for the test. It&#8217;s just like configuring an regular @NgModule from scratch, but you just add what you need.\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nimport { async, TestBed } from '@angular\/core\/testing';\r\n\r\nbeforeEach(async(() =&gt; {\r\n  TestBed.configureTestingModule({\r\n    declarations: &#x5B; AppComponent ],\r\n    providers: &#x5B;],\r\n    imports: &#x5B;]\r\n  })\r\n  .compileComponents();\r\n}));\r\n<\/pre>\n<\/li>\n<li>Using <strong>TestBed.createComponent<\/strong> create component instance returns componentFixture Interface. componentFixture is a wrapper over component with additional methods needed for testing. <strong>fixture.componentInstance<\/strong> returns instance of component.\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nbeforeEach(() =&gt; {\r\n.\r\n\/\/Returns Component Fixture\r\nlet fixture = TestBed.createComponent(EmployeeComponent);\r\n\r\n\/\/Get Instance of Component\r\ncomponent = fixture.componentInstance;\r\n.\r\n.\r\n});\r\n<\/pre>\n<\/li>\n<li>fixture.detectChanges() would allow to check for change in DOM elements before expect or whenever it is referenced for value<\/li>\n<li>DebugElement, an interface  which wraps native element instead of HTML element in idle scenario<\/li>\n<li>schemas:[NO_ERRORS_SCHEMAS] lets angular testing module to ignore unknown tags in html during testing. Suppose you have used router tags in html<br \/>\nand angular could not recognize the router tag since routerTestingModule is not imported you can use this tag as below to avoid errors.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nbeforeEach(() =&gt; {\r\n     TestBed.configureTestingModule({\r\n      declarations: &#x5B; EmployeeComponent]\r\n      schemas:&#x5B;NO_ERRORS_SCHEMAS]\r\n     }).compileComponents();\r\n\r\n    \/\/Testbed to create fixture for component\r\n    fixture = TestBed.createComponent(EmployeeComponent);\r\n\r\n    \/\/Using fixture to create component and service \r\n    component = fixture.componentInstance;\r\n    empService = TestBed.inject(EmpserviceService);    \r\n });\r\n\r\n<\/pre>\n<\/li>\n<\/ol>\n<p><strong class=\"ctaHeader2\">Simple testing with matchers<\/strong><\/p>\n<ol>\n<li><strong>toBeTruthy <\/strong>tells the component is created successfully<\/li>\n<li><strong>toBe<\/strong> checks for equality of value<\/li>\n<li><strong>toEqual<\/strong> method does object comparison<\/li>\n<li><strong>toContain<\/strong> method checks whether value is available in array<\/li>\n<\/ol>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfit('Simple component testing', ()=&gt;{\r\n  \/\/Checks whether component has been created\r\n  expect(component).toBeTruthy();\r\n\r\n  \/\/Checks whether value returned by function are same\r\n  expect(component.getGender(true)).toBe('Male');\r\n\r\n  \/\/Checks objects are equal and array contain that element\r\n  var Names = &#x5B;'Name1', 'Name2', 'Name3'];\r\n  expect(Names).toEqual(&#x5B;'Name1', 'Name2', 'Name3']);\r\n  expect(Names).toContain('Name2');\r\n});\r\n<\/pre>\n<p><strong class=\"ctaHeader2\">Checking DOM Elements<\/strong><\/p>\n<ol>\n<li><strong>nativeElement<\/strong> &#8211; provides the access to DOM element.This is same as Javascript DOM Element we use for DOM Manipulation.Using nativeElement you can<br \/>\naccess all the API methods provided vy Javascript for DOM manipulation like QuerySelector<\/li>\n<li><strong>fixture.detectChanges()<\/strong> &#8211; updates the DOM Element after setting the value from testing component. This is similar to refresh once the component value is set the html should be refreshed.<\/li>\n<li><strong>debugElement<\/strong>&#8211; debugElement is similar to nativeElement which has wrapper with some additional methods.It has query, queryAll, queryNode methods<br \/>\ndebugElement inturn calls nativeElement to get the values.<strong>fixture.debugElement<\/strong> returns the root debugElement from which we query for the nativeElement.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\nfixture.debugElement.query(By.css('a')).nativeElement.textContent)\r\nfixture.debugElement.query(By.css('#ContactUsId')).nativeElement.textContent)\r\nfixture.debugElement.query(By.css('.ContactUsId')).nativeElement.textContent)\r\n.\r\n.\r\n.\r\n<\/pre>\n<\/li>\n<li>The below statement end up the same anchor tag\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n.\r\n\r\nfixture.debugElement.query(By.css('a')).nativeElement.textContent)\r\nfixture.nativeElement.querySelector('a').textContent\r\n.\r\n<\/pre>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Before start testing method in components we initialize few things as below beforeEach method helps in initializing the values needed for testing describe are similar to JUnit class and it are similar to junit class methods which tests particular method in src code TestBed is needed while testing both component and html.You need to configure&hellip; <a href=\"https:\/\/codethataint.com\/blog\/how-to-test-typescript-methods-using-spyon\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[318],"tags":[],"class_list":["post-3862","post","type-post","status-publish","format-standard","hentry","category-jasmine"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3862","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=3862"}],"version-history":[{"count":6,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3862\/revisions"}],"predecessor-version":[{"id":3899,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3862\/revisions\/3899"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}