-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(mock-intance): graceful reset after specs
- Loading branch information
Showing
8 changed files
with
347 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { MockInstance } from 'ng-mocks'; | ||
|
||
class TargetService { | ||
protected name = 'target'; | ||
|
||
public echo1(): string { | ||
return this.name; | ||
} | ||
|
||
public echo2(): string { | ||
return this.name; | ||
} | ||
} | ||
|
||
// Accepts valid type. | ||
MockInstance(TargetService, () => ({ | ||
echo1: () => 'mock', | ||
})); | ||
// Accepts valid type. | ||
MockInstance(TargetService, { | ||
init: () => ({ | ||
echo2: () => 'mock', | ||
}), | ||
}); | ||
// Accepts internal change. | ||
MockInstance(TargetService, instance => { | ||
instance.echo1 = () => 'mock'; | ||
}); | ||
// Accepts internal change. | ||
MockInstance(TargetService, { | ||
init: instance => { | ||
instance.echo1 = () => 'mock'; | ||
}, | ||
}); | ||
// Accepts reset. | ||
MockInstance(TargetService); | ||
|
||
// @ts-expect-error: does not support wrong types. | ||
MockInstance(TargetService, () => true); | ||
// @ts-expect-error: does not support wrong types. | ||
MockInstance(TargetService, () => ({ | ||
prop: 123, | ||
})); | ||
MockInstance(TargetService, { | ||
// @ts-expect-error: does not support wrong types. | ||
init: () => ({ | ||
prop: 123, | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
import { MockInstance } from 'ng-mocks'; | ||
|
||
const TOKEN = new InjectionToken<{ prop: boolean }>('TOKEN'); | ||
|
||
// Accepts valid type. | ||
MockInstance(TOKEN, () => ({ | ||
prop: true, | ||
})); | ||
// Accepts valid type. | ||
MockInstance(TOKEN, { | ||
init: () => ({ | ||
prop: false, | ||
}), | ||
}); | ||
// Accepts reset. | ||
MockInstance(TOKEN); | ||
|
||
// @ts-expect-error: does not support wrong types. | ||
MockInstance(TOKEN, () => true); | ||
// @ts-expect-error: does not support wrong types. | ||
MockInstance(TOKEN, () => ({ | ||
prop: 123, | ||
})); | ||
// @ts-expect-error: does not support wrong types. | ||
MockInstance(TOKEN, { | ||
init: () => ({ | ||
prop: 123, | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { | ||
MockBuilder, | ||
MockInstance, | ||
MockReset, | ||
ngMocks, | ||
} from 'ng-mocks'; | ||
|
||
@Injectable() | ||
class TargetService { | ||
protected name = 'target'; | ||
|
||
public echo(): string { | ||
return this.name; | ||
} | ||
} | ||
|
||
describe('mock-instance-in-it', () => { | ||
beforeAll(() => | ||
MockInstance(TargetService, () => ({ | ||
echo: () => 'beforeAll', | ||
})), | ||
); | ||
|
||
afterAll(() => { | ||
// need to call it remove cached TargetService. | ||
ngMocks.flushTestBed(); | ||
|
||
const actual = TestBed.get(TargetService).echo(); | ||
expect(actual).toEqual('beforeAll'); | ||
|
||
MockReset(); | ||
}); | ||
|
||
beforeEach(() => MockBuilder().mock(TargetService)); | ||
|
||
beforeEach(() => | ||
MockInstance(TargetService, () => ({ | ||
echo: () => 'beforeEach', | ||
})), | ||
); | ||
|
||
it('overrides in the 1st it', () => { | ||
MockInstance(TargetService, () => ({ | ||
echo: () => 'it', | ||
})); | ||
|
||
const actual = TestBed.get(TargetService).echo(); | ||
expect(actual).toEqual('it'); | ||
}); | ||
|
||
it('uses beforeEach in the 2nd it', () => { | ||
const actual = TestBed.get(TargetService).echo(); | ||
expect(actual).toEqual('beforeEach'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { | ||
Inject, | ||
Injectable, | ||
InjectionToken, | ||
NgModule, | ||
} from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { MockBuilder, MockInstance } from 'ng-mocks'; | ||
|
||
const TOKEN = new InjectionToken<string>('TOKEN'); | ||
|
||
@Injectable() | ||
class TargetService { | ||
public constructor( | ||
@Inject(TOKEN) protected readonly name: string, | ||
) {} | ||
|
||
public echo(): string { | ||
return this.name; | ||
} | ||
} | ||
|
||
@NgModule({ | ||
providers: [ | ||
TargetService, | ||
{ | ||
provide: TOKEN, | ||
useValue: 'target', | ||
}, | ||
], | ||
}) | ||
class TargetModule {} | ||
|
||
describe('mock-instance-token', () => { | ||
beforeEach(() => MockBuilder(TargetService, TargetModule)); | ||
|
||
it('provides tokens', () => { | ||
MockInstance(TOKEN, () => 'mock'); | ||
|
||
const actual = TestBed.get(TargetService).echo(); | ||
expect(actual).toEqual('mock'); | ||
}); | ||
}); |