How to use MY_TOKEN_MULTI method in ng-mocks

Best JavaScript code snippet using ng-mocks

test.spec.ts

Source: test.spec.ts Github

copy

Full Screen

1import { VERSION } from '@angular/​core';2import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';3import {4 MY_TOKEN_MULTI,5 MY_TOKEN_SINGLE,6 TargetComponent,7 TargetModule,8} from './​fixtures';9/​/​ Because all tokens have factories the test should render them correctly.10/​/​ There is no way to specify multi in a factory, so we do not get an array.11describe('module-with-factory-tokens:real', () => {12 if (Number.parseInt(VERSION.major, 10) <= 5) {13 it('a5', () => {14 /​/​ pending('Need Angular > 5');15 expect(true).toBeTruthy();16 });17 return;18 }19 beforeEach(() =>20 MockBuilder()21 .keep(TargetModule)22 .keep(MY_TOKEN_SINGLE)23 .keep(MY_TOKEN_MULTI),24 );25 it('renders all tokens', () => {26 const fixture = MockRender(TargetComponent);27 expect(fixture.nativeElement.innerHTML).toEqual(28 '<internal-component>"MY_TOKEN_SINGLE" "MY_TOKEN_MULTI"</​internal-component>',29 );30 });31});32/​/​ Because all tokens are kept the test should render them correctly.33/​/​ There is no way to specify multi in a factory, so we do not get an array.34describe('module-with-factory-tokens:keep', () => {35 if (Number.parseInt(VERSION.major, 10) <= 5) {36 it('a5', () => {37 /​/​ pending('Need Angular > 5');38 expect(true).toBeTruthy();39 });40 return;41 }42 beforeEach(() =>43 MockBuilder(44 [TargetComponent, MY_TOKEN_SINGLE, MY_TOKEN_MULTI],45 TargetModule,46 ),47 );48 it('renders all tokens', () => {49 const fixture = MockRender(TargetComponent);50 expect(fixture.nativeElement.innerHTML).toEqual(51 '<internal-component>"MY_TOKEN_SINGLE" "MY_TOKEN_MULTI"</​internal-component>',52 );53 });54});55/​/​ Preferred way.56/​/​ Because tokens are provided in the testbed module with custom values the test should render them.57describe('module-with-factory-tokens:mock-0', () => {58 beforeEach(() =>59 MockBuilder(TargetComponent, TargetModule)60 .provide({61 provide: MY_TOKEN_SINGLE,62 useValue: 'V1',63 })64 .provide({65 multi: true,66 provide: MY_TOKEN_MULTI,67 useValue: 'V2',68 }),69 );70 it('fails to render all tokens', () => {71 const fixture = MockRender(TargetComponent);72 expect(ngMocks.formatHtml(fixture)).toContain('"V1" [ "V2" ]');73 });74});75/​/​ Because all tokens are replaced with mock copies in the module the test should render empty values.76/​/​ The tokens will be added to provides with undefined values.77/​/​ Result of the render is an empty string because there is no way to pass multi.78describe('module-with-factory-tokens:mock-1', () => {79 beforeEach(() =>80 MockBuilder(TargetComponent, [81 TargetModule,82 MY_TOKEN_SINGLE,83 MY_TOKEN_MULTI,84 ]),85 );86 it('renders all tokens', () => {87 const fixture = MockRender(TargetComponent);88 expect(ngMocks.formatHtml(fixture)).toEqual(89 '<internal-component></​internal-component>',90 );91 });92});93/​/​ Because all tokens are replaced with mock copies with custom values the test should render them.94/​/​ There is no way to specify multi in a factory, so we do not get an array.95describe('module-with-factory-tokens:mock-2', () => {96 beforeEach(() =>97 MockBuilder(TargetComponent, TargetModule)98 .mock(MY_TOKEN_SINGLE, 'MOCK_MY_TOKEN_SINGLE', { export: true })99 .mock(MY_TOKEN_MULTI, 'MOCK_MY_TOKEN_MULTI', { export: true }),100 );101 it('renders all tokens', () => {102 const fixture = MockRender(TargetComponent);103 expect(fixture.nativeElement.innerHTML).toEqual(104 '<internal-component>"MOCK_MY_TOKEN_SINGLE" "MOCK_MY_TOKEN_MULTI"</​internal-component>',105 );106 });107});108/​/​ And the most interesting case. Because we do not touch tokens at all and mock the module109/​/​ the tokens will used as they are with their factories.110/​/​ Unfortunately, it is quite tough to guess which tokens we can keep, mocks or omit and now111/​/​ a user is responsible to specify tokens for his mock.112/​/​ UPD 2020-10-28: it has been fixed. Now all missed tokens are added to the TestModuleMeta,113/​/​ therefore we have to keep them.114describe('module-with-factory-tokens:mock-3', () => {115 if (Number.parseInt(VERSION.major, 10) <= 5) {116 it('a5', () => {117 /​/​ pending('Need Angular > 5');118 expect(true).toBeTruthy();119 });120 return;121 }122 beforeEach(() =>123 MockBuilder(124 [TargetComponent, MY_TOKEN_SINGLE, MY_TOKEN_MULTI],125 TargetModule,126 ),127 );128 it('renders all tokens', () => {129 const fixture = MockRender(TargetComponent);130 expect(fixture.nativeElement.innerHTML).toEqual(131 '<internal-component>"MY_TOKEN_SINGLE" "MY_TOKEN_MULTI"</​internal-component>',132 );133 });...

Full Screen

Full Screen

fixtures.ts

Source: fixtures.ts Github

copy

Full Screen

1import { CommonModule } from '@angular/​common';2import { Component, Inject, InjectionToken, NgModule } from '@angular/​core';3export const MY_TOKEN_SINGLE = new InjectionToken('MY_TOKEN_SINGLE');4export const MY_TOKEN_MULTI = new InjectionToken('MY_TOKEN_MULTI');5@Component({6 selector: 'internal-component',7 template: '{{ tokenSingle | json }} {{ tokenMulti | json }}',8})9export class TargetComponent {10 public constructor(11 @Inject(MY_TOKEN_SINGLE) public readonly tokenSingle: string,12 @Inject(MY_TOKEN_MULTI) public readonly tokenMulti: string[],13 ) {}14}15@NgModule({16 declarations: [TargetComponent],17 exports: [TargetComponent],18 imports: [CommonModule],19 providers: [20 {21 provide: MY_TOKEN_SINGLE,22 useValue: 'MY_TOKEN_SINGLE',23 },24 {25 multi: true,26 provide: MY_TOKEN_MULTI,27 useValue: 'MY_TOKEN_MULTI',28 },29 {30 multi: true,31 provide: MY_TOKEN_MULTI,32 useValue: 'MY_TOKEN_MULTI_2',33 },34 ],35})...

Full Screen

Full Screen

Blogs

Check out the latest blogs from LambdaTest on this topic:

Feeding your QA Career – Developing Instinctive &#038; Practical Skills

The QA testing profession requires both educational and long-term or experience-based learning. One can learn the basics from certification courses and exams, boot camp courses, and college-level courses where available. However, developing instinctive and practical skills works best when built with work experience.

Running Tests In Cypress With GitHub Actions [Complete Guide]

In today’s tech world, where speed is the key to modern software development, we should aim to get quick feedback on the impact of any change, and that is where CI/CD comes in place.

Your Favorite Dev Browser Has Evolved! The All New LT Browser 2.0

We launched LT Browser in 2020, and we were overwhelmed by the response as it was awarded as the #5 product of the day on the ProductHunt platform. Today, after 74,585 downloads and 7,000 total test runs with an average of 100 test runs each day, the LT Browser has continued to help developers build responsive web designs in a jiffy.

Using ChatGPT for Test Automation

ChatGPT broke all Internet records by going viral in the first week of its launch. A million users in 5 days are unprecedented. A conversational AI that can answer natural language-based questions and create poems, write movie scripts, write social media posts, write descriptive essays, and do tons of amazing things. Our first thought when we got access to the platform was how to use this amazing platform to make the lives of web and mobile app testers easier. And most importantly, how we can use ChatGPT for automated testing.

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run ng-mocks automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful