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 prefer-selector-in-select rule #3991

Merged
merged 3 commits into from
Aug 11, 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
152 changes: 148 additions & 4 deletions modules/eslint-plugin/spec/rules/prefer-selector-in-select.spec.ts
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 test$ = somethingOutside();
Expand Down Expand Up @@ -74,7 +74,58 @@ class Ok6 {
}`,
];

const invalid: () => RunTests['invalid'] = () => [
const validInject: () => RunTests['valid'] = () => [
`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'

class Ok7 {
store = inject(Store)
view$ = this.store.pipe(select(selectCustomers))
}`,
`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'

class Ok8 {
private store = inject(Store)
view$ = this.store.select(selectCustomers)
}`,
`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'

class Ok9 {
private store = inject(Store)
view$ = this.store.pipe(select(CustomerSelectors.selectCustomers))
}`,
`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'

class Ok10 {
private readonly store = inject(Store)
view$ = this.store.select(selectCustomers)
}`,
`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'

class Ok11 {
private store = inject(Store)
view$ = this.store.pipe(select(selectQueryParam('parameter')))
}`,
`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'

class Ok12 {
private readonly store = inject(Store)
view$ = this.store.select(this.store.select(hasAuthorization, 'ADMIN'));
}`,
];

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

const invalidInject: () => RunTests['invalid'] = () => [
fromFixture(
`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'

class NotOk8 {
store = inject(Store)
view$ = this.store.pipe(select('customers'))
~~~~~~~~~~~ [${messageId}]
}`
),
fromFixture(
`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'

class NotOk9 {
private store = inject(Store)
view$ = this.store.select('customers')
~~~~~~~~~~~ [${messageId}]
}`
),
fromFixture(
`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'

class NotOk10 {
private readonly store = inject(Store)
view$ = this.store.pipe(select('customers', 'orders'))
~~~~~~~~~~~ [${messageId}]
~~~~~~~~ [${messageId}]
}`
),
fromFixture(
`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'

class NotOk11 {
private store = inject(Store)
view$ = this.store.select('customers', 'orders')
~~~~~~~~~~~ [${messageId}]
~~~~~~~~ [${messageId}]
}`
),
fromFixture(
`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'

class NotOk12 {
private store = inject(Store)
view$ = this.store.pipe(select(s => s.customers))
~~~~~~~~~~~~~~~~ [${messageId}]
}`
),
fromFixture(
`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'

class NotOk13 {
readonly store = inject(Store)
view$ = this.store.select(s => s.customers)
~~~~~~~~~~~~~~~~ [${messageId}]
}`
),
fromFixture(
`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'

class NotOk14 {
private store$ = inject(Store)
view$ = this.store$.select(s => s.customers)
~~~~~~~~~~~~~~~~ [${messageId}]
}`
),
fromFixture(
`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'

class NotOk15 {
private readonly store$ = inject(Store)
view$ = this.store$.pipe(select('customers'))
~~~~~~~~~~~ [${messageId}]
}`
),
];

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