From eb077550c52c31d28c58fe24c91ed9166b2d04a7 Mon Sep 17 00:00:00 2001 From: suke Date: Tue, 22 Aug 2023 02:49:33 +0900 Subject: [PATCH] test(eslint-plugin): add inject tests for updater-explicit-return-type rule (#4013) --- .../updater-explicit-return-type.spec.ts | 86 ++++++++++++++++++- 1 file changed, 82 insertions(+), 4 deletions(-) diff --git a/modules/eslint-plugin/spec/rules/updater-explicit-return-type.spec.ts b/modules/eslint-plugin/spec/rules/updater-explicit-return-type.spec.ts index bb1b5a12e0..19359533a0 100644 --- a/modules/eslint-plugin/spec/rules/updater-explicit-return-type.spec.ts +++ b/modules/eslint-plugin/spec/rules/updater-explicit-return-type.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'] = () => [ ` import { ComponentStore } from '@ngrx/component-store' @@ -66,7 +66,38 @@ class Ok3 { }`, ]; -const invalid: () => RunTests['invalid'] = () => [ +const validInject: () => RunTests['valid'] = () => [ + ` +import { ComponentStore } from '@ngrx/component-store' +import { inject } from '@angular/core' + +class Ok4 { + private readonly store = inject(ComponentStore); + readonly addMovie = this.store.updater( + (state, movie): MoviesState => ({ + movies: [...state.movies, movie], + }), + ) +}`, + ` +import { ComponentStore } from '@ngrx/component-store' +import { inject } from '@angular/core' + +class Ok5 { + readonly addMovie: Observable + customStore = inject(ComponentStore) + + constructor() { + this.addMovie = this.customStore.updater( + (state, movie): MoviesState => ({ + movies: [...state.movies, movie], + }), + ) + } +}`, +]; + +const invalidConstructor: () => RunTests['invalid'] = () => [ fromFixture(` import { ComponentStore } from '@ngrx/component-store' @@ -135,7 +166,54 @@ export class CompetitorsStore2 extends CompetitorsStore1 { }`), ]; +const invalidInject: () => RunTests['invalid'] = () => [ + fromFixture(` +import { ComponentStore } from '@ngrx/component-store' +import { inject } from '@angular/core' + +class NotOk4 { + componentStore = inject(ComponentStore) + readonly updateMovie: Observable + + constructor() { + this.updateMovie = this.componentStore.updater(() => ({ movies: MOVIES })) + ~~~~~~~~~~~~~~~~~~~~~~~~~~ [${messageId}] + } +}`), + fromFixture(` +import { ComponentStore } from '@ngrx/component-store' +import { inject } from '@angular/core' + +class NotOk5 { + private readonly store = inject(ComponentStore) + readonly addMovie = this.store.updater((state, movie) => ({ movies: [...state.movies, movie] })) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [${messageId}] +}`), + fromFixture(` +import { ComponentStore } from '@ngrx/component-store' +import { inject } from '@angular/core' + +class NotOk6 { + customStore = inject(ComponentStore) + private readonly store = inject(ComponentStore) + readonly addMovie: Observable + readonly updateMovie: Observable + + constructor() { + this.addMovie = this.customStore.updater((state, movie) => ({ movies: [...state.movies, movie] })) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [${messageId}] + this.updateMovie = this.store.updater(() => ({ movies: MOVIES })) + ~~~~~~~~~~~~~~~~~~~~~~~~~~ [${messageId}] + } + + ngOnInit() { + const updater = (item: Movie) => item + updater() + } +}`), +]; + ruleTester().run(path.parse(__filename).name, rule, { - valid: valid(), - invalid: invalid(), + valid: [...validConstructor(), ...validInject()], + invalid: [...invalidConstructor(), ...invalidInject()], });