Skip to content

Commit ed36114

Browse files
committed
feat: add ngtx.scenario.testGeneratorFn
1 parent 6f81f29 commit ed36114

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

library/ngtx.ts

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from './scenario-testing/symbols';
88
import {
99
ComponentFixtureRef,
10+
NgtxScenarioTestAssertionFn,
1011
ScenarioSetupFn,
1112
ScenarioViewSetupFn,
1213
} from './scenario-testing/types';
@@ -69,10 +70,17 @@ export const ngtx = Object.assign(_ngtx, {
6970
scenario: {
7071
envSetupFn,
7172
viewSetupFn,
73+
testGeneratorFn,
7274
},
7375
is,
7476
});
7577

78+
function testGeneratorFn<Html extends HTMLElement, Component>(
79+
fn: NgtxScenarioTestAssertionFn<Html, Component>,
80+
) {
81+
return fn;
82+
}
83+
7684
function envSetupFn(fn: () => unknown) {
7785
return Object.assign(fn, { [NgtxScenarioSetupFnMarker]: true });
7886
}

library/specs/scenario/scenario-testing.spec.ts

+17-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { ActivatedRoute, RouterModule } from '@angular/router';
44
import { ngtx } from '../../ngtx';
55
import { ScenarioTestingHarness } from '../../scenario-testing/scenario-harnesses';
66
import { useScenarioTesting } from '../../scenario-testing/scenario-testing';
7-
import { NgtxScenarioTestAssertionFn } from '../../scenario-testing/types';
87

98
@Injectable()
109
class MyService {
@@ -121,15 +120,25 @@ expectThat(
121120
the.Text.toHaveState({ text: 'Hello, World!' }),
122121
);
123122

124-
const beComponentType =
125-
(type: any): NgtxScenarioTestAssertionFn<HTMLElement, any> =>
123+
const beComponentType = (type: any) =>
126124
// TODO: docs: document that debugElement must only be accessed within it case:
127-
(addTests, harness) =>
125+
ngtx.scenario.testGeneratorFn((addTests, harness) =>
128126
addTests(() => {
129-
it(`should be the component "${type.name}"`, () => {
130-
expect(harness.debugElement.componentInstance).toBeInstanceOf(type);
127+
const description = harness.isAssertionNegated
128+
? `should not be the component "${type.name}"`
129+
: `should be the component "${type.name}"`;
130+
131+
it(description, () => {
132+
if (harness.isAssertionNegated) {
133+
expect(harness.debugElement.componentInstance).not.toBeInstanceOf(
134+
type,
135+
);
136+
} else {
137+
expect(harness.debugElement.componentInstance).toBeInstanceOf(type);
138+
}
131139
});
132-
});
140+
}),
141+
);
133142

134143
expectThat(
135144
the.Div.toBeFound(),
@@ -140,6 +149,7 @@ expectThat(
140149
beComponentType(TextComponent),
141150
beComponentType(TextComponent),
142151
),
152+
the.Text.not.to(beComponentType(ScenarioTestComponent)),
143153
);
144154

145155
tests.run();

0 commit comments

Comments
 (0)