Skip to content

Commit

Permalink
test(eslint-plugin): add inject tests for prefer-action-creator-in-di…
Browse files Browse the repository at this point in the history
…spatch rule (#4004)
  • Loading branch information
suke authored Aug 14, 2023
1 parent c0a9c29 commit 067b2b8
Showing 1 changed file with 105 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type MessageIds = ESLintUtils.InferMessageIdsTypeFromRule<typeof rule>;
type Options = ESLintUtils.InferOptionsTypeFromRule<typeof rule>;
type RunTests = TSESLint.RunTests<MessageIds, Options>;

const valid: () => RunTests['valid'] = () => [
const validConstructor: () => RunTests['valid'] = () => [
`
class Ok {
readonly viewModel$ = somethingElse()
Expand Down Expand Up @@ -49,7 +49,45 @@ class Ok3 {
}`,
];

const invalid: () => RunTests['invalid'] = () => [
const validInject: () => RunTests['valid'] = () => [
`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class Ok4 {
store$ = inject(Store)
constructor() {
this.store$.dispatch(action)
}
}`,
`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class Ok5 {
private readonly store$ = inject(Store)
constructor() {
this.store$.dispatch(BookActions.load())
}
}`,
`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class Ok6 {
private readonly store = inject(Store)
constructor() {
this.store.dispatch(login({ payload }));
this.store.dispatch(AuthActions.dispatch({ type: 'SUCCESS' }));
nonStore.dispatch(AuthActions.dispatch({ type: 'FAIL' }));
}
}`,
];

const invalidConstructor: () => RunTests['invalid'] = () => [
fromFixture(`
import { Store } from '@ngrx/store'
Expand Down Expand Up @@ -99,7 +137,70 @@ class NotOk3 {
}`),
];

const invalidInject: () => RunTests['invalid'] = () => [
fromFixture(`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class NotOk4 {
store$ = inject(Store)
constructor() {
this.store$.dispatch(new CustomAction())
~~~~~~~~~~~~~~~~~~ [${messageId}]
}
}`),
fromFixture(`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class NotOk5 {
private readonly store$ = inject(Store)
constructor() {
this.store$.dispatch({ type: 'custom' })
~~~~~~~~~~~~~~~~~~ [${messageId}]
}
}`),
fromFixture(`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class NotOk6 {
store = inject(Store)
private readonly store$ = inject(Store)
constructor() {
this.store.dispatch(new Login({ payload }));
~~~~~~~~~~~~~~~~~~~~~~ [${messageId}]
this.store$.dispatch(new AuthActions.dispatch({ type: 'SUCCESS' }));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [${messageId}]
nonStore.dispatch(new AuthActions.dispatch({ type: 'FAIL' }));
}
ngOnInit() {
const store = { dispatch: () => void 0 }
store.dispatch()
}
}`),
fromFixture(`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class NotOk7 {
private readonly store$ = inject(Store)
constructor() {}
ngOnInit() {
this.store$.dispatch(useObject ? { type: 'custom' } : new CustomAction())
~~~~~~~~~~~~~~~~~~ [${messageId}]
~~~~~~~~~~~~~~~~~~ [${messageId}]
}
}`),
];

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

0 comments on commit 067b2b8

Please sign in to comment.