diff --git a/modules/eslint-plugin/spec/rules/prefer-action-creator-in-dispatch.spec.ts b/modules/eslint-plugin/spec/rules/prefer-action-creator-in-dispatch.spec.ts index 0307663e13..e7e4d3a326 100644 --- a/modules/eslint-plugin/spec/rules/prefer-action-creator-in-dispatch.spec.ts +++ b/modules/eslint-plugin/spec/rules/prefer-action-creator-in-dispatch.spec.ts @@ -13,7 +13,7 @@ type MessageIds = ESLintUtils.InferMessageIdsTypeFromRule; type Options = ESLintUtils.InferOptionsTypeFromRule; type RunTests = TSESLint.RunTests; -const valid: () => RunTests['valid'] = () => [ +const validConstructor: () => RunTests['valid'] = () => [ ` class Ok { readonly viewModel$ = somethingElse() @@ -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' @@ -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()], });