Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(eslint-plugin): add tests using inject #4022

Merged
merged 2 commits into from
Sep 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 173 additions & 4 deletions modules/eslint-plugin/spec/rules/avoid-cyclic-effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { EffectConfig } from '@ngrx/effects'
import { Actions, createEffect, ofType } from '@ngrx/effects'
import { createAction } from '@ngrx/store'
import { map, tap } from 'rxjs/operators'
import { inject } from '@angular/core';
const foo = createAction('FOO')
const bar = createAction('BAR')
Expand All @@ -30,7 +31,7 @@ const genericFoo = createAction(\`$\{subject} FOO\`)
const genericBar = createAction(\`$\{subject} BAR\`)
`;

const valid: () => RunTests['valid'] = () => [
const validConstructor: () => RunTests['valid'] = () => [
`
${setup}
class Effect {
Expand Down Expand Up @@ -152,7 +153,105 @@ class Effect {
}`,
];

const invalid: () => RunTests['invalid'] = () => [
const validInject: () => RunTests['valid'] = () => [
`
${setup}
class Effect {
private actions$ = inject(Actions);
foo$ = createEffect(() =>
this.actions$.pipe(
ofType(foo),
map(() => bar()),
),
)
}`,
`
${setup}
class Effect {
private actions$ = inject(Actions);
foo$ = createEffect(() => {
return this.actions$.pipe(
ofType(foo),
map(() => bar()),
)
})
}`,
`
${setup}
class Effect {
private actions$ = inject(Actions);
foo$ = createEffect(() => {
return this.actions$.pipe(
ofType(fromFoo.foo),
map(() => fromFoo.bar()),
)
})
}`,
`
${setup}
class Effect {
private actions = inject(Actions);
foo$ = createEffect(() => {
return this.actions.pipe(
ofType(foo),
mapTo(bar()),
)
})
}`,
`
${setup}
class Effect {
private actions$ = inject(Actions);
foo$ = createEffect(() => {
return this.actions$.pipe(
ofType(foo),
tap(() => alert('hi'))
)
}, { dispatch: false }
)
}`,
`
${setup}
class Effect {
foo$: CreateEffectMetadata
private actions$ = inject(Actions);
constructor() {
this.foo$ = createEffect(() =>
this.actions$.pipe(
ofType(genericFoo),
map(() => genericBar()),
),
)
}
}`,
`
${setup}
class Effect {
private actions$ = otherInject(Actions);
foo$ = createEffect(() => {
return this.actions$.pipe(
ofType(foo),
tap(() => alert('hi'))
)
}, { dispatch: false }
)
}`,
`
${setup}
class Effect {
private actions$ = inject(OtherActions);
foo$ = createEffect(() => {
return this.actions$.pipe(
ofType(foo),
tap(() => alert('hi'))
)
}, { dispatch: false }
)
}`,
];

const invalidConstructor: () => RunTests['invalid'] = () => [
fromFixture(`
${setup}
class Effect {
Expand Down Expand Up @@ -266,7 +365,77 @@ class Effect {
}`),
];

const invalidInject: () => RunTests['invalid'] = () => [
fromFixture(`
${setup}
class Effect {
private actions$ = inject(Actions);
foo$ = createEffect(() =>
this.actions$.pipe(
~~~~~~~~~~~~~~~~~~ [${messageId}]
ofType(foo),
tap(() => alert('hi'))
),
)
}`),
fromFixture(`
${setup}
class Effect {
private actions$ = inject(Actions);
foo$ = createEffect(() => {
return this.actions$.pipe(
~~~~~~~~~~~~~~~~~~ [${messageId}]
ofType(foo),
tap(() => alert('hi'))
)
})
}`),
fromFixture(`
${setup}
class Effect {
private actions$ = inject(Actions);
foo$ = createEffect(() => {
return this.actions$.pipe(
~~~~~~~~~~~~~~~~~~ [${messageId}]
ofType(foo),
tap(() => alert('hi'))
)
}, { dispatch: true });
}`),
fromFixture(`
${setup}
class Effect {
private actions$ = inject(Actions);
foo$ = createEffect(
() =>
({ debounce = 100 } = {}) =>
debounce
? this.actions$.pipe(
~~~~~~~~~~~~~~~~~~ [${messageId}]
ofType(fromFoo.foo),
tap(() => alert('hi')),
)
: this.actions$.pipe(),
);
}`),
fromFixture(`
${setup}
class Effect {
foo$: CreateEffectMetadata
private actions$ = inject(Actions);
constructor() {
this.foo$ = createEffect(() =>
this.actions$.pipe(
~~~~~~~~~~~~~~~~~~ [${messageId}]
ofType(genericFoo),
),
)
}
}`),
];

ruleTester().run(path.parse(__filename).name, rule, {
valid: valid(),
invalid: invalid(),
valid: [...validConstructor(), ...validInject()],
invalid: [...invalidConstructor(), ...invalidInject()],
});