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 inject tests for updater-explicit-return-type rule #4013

Merged
merged 1 commit into from
Aug 21, 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
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'] = () => [
`
import { ComponentStore } from '@ngrx/component-store'

Expand Down Expand Up @@ -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<MoviesState>);
readonly addMovie = this.store.updater<Movie>(
(state, movie): MoviesState => ({
movies: [...state.movies, movie],
}),
)
}`,
`
import { ComponentStore } from '@ngrx/component-store'
import { inject } from '@angular/core'

class Ok5 {
readonly addMovie: Observable<unknown>
customStore = inject(ComponentStore<MoviesState>)

constructor() {
this.addMovie = this.customStore.updater<Movie>(
(state, movie): MoviesState => ({
movies: [...state.movies, movie],
}),
)
}
}`,
];

const invalidConstructor: () => RunTests['invalid'] = () => [
fromFixture(`
import { ComponentStore } from '@ngrx/component-store'

Expand Down Expand Up @@ -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<MoviesState>)
readonly updateMovie: Observable<unknown>

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<MoviesState>)
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<MoviesState>)
private readonly store = inject(ComponentStore<MoviesState>)
readonly addMovie: Observable<unknown>
readonly updateMovie: Observable<unknown>

constructor() {
this.addMovie = this.customStore.updater<Movie>((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()],
});