Skip to content

Commit

Permalink
test(eslint-plugin): add inject tests for prefer-selector-in-select r…
Browse files Browse the repository at this point in the history
…ule (#3991)

Co-authored-by: Tim Deschryver <28659384+timdeschryver@users.noreply.github.com>
  • Loading branch information
suke and timdeschryver authored Aug 11, 2023
1 parent d2502fb commit c0a9c29
Showing 1 changed file with 148 additions and 4 deletions.
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()],
});

0 comments on commit c0a9c29

Please sign in to comment.