Skip to content

Commit

Permalink
test(eslint-plugin): add inject tests for select-style rule
Browse files Browse the repository at this point in the history
  • Loading branch information
suke committed Jul 30, 2023
1 parent 27e7599 commit cbc9b20
Showing 1 changed file with 243 additions and 4 deletions.
247 changes: 243 additions & 4 deletions modules/eslint-plugin/spec/rules/select-style.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type MessageIds = ESLintUtils.InferMessageIdsTypeFromRule<typeof rule>;
type Options = readonly ESLintUtils.InferOptionsTypeFromRule<typeof rule>[0][];
type RunTests = TSESLint.RunTests<MessageIds, Options>;

const valid: () => RunTests['valid'] = () => [
const validConstructor: () => RunTests['valid'] = () => [
`
import { Store } from '@ngrx/store'
Expand Down Expand Up @@ -94,7 +94,79 @@ class Ok9 {
},
];

const invalid: () => RunTests['invalid'] = () => [
const validInject: () => RunTests['valid'] = () => [
`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class Ok10 {
private store = inject(Store)
}`,
`
import { Store, select } from '@ngrx/store'
import { inject } from '@angular/core'
class Ok11 {
private store = inject(Store)
}`,
`
import { Store, select } from '@ngrx/store'
import { inject } from '@angular/core'
class Ok12 {
private store = inject(Store)
foo$ = select(selector)
}`,
`
import { select } from '@my-org/framework'
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class Ok13 {
private store = inject(Store)
foo$ = this.store.pipe(select(selector))
}`,
`
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class Ok14 {
private store = inject(Store)
foo$ = this.store.select(selector)
}`,
`
import { Store, select } from '@ngrx/store'
import { inject } from '@angular/core'
class Ok15 {
private customName = inject(Store)
foo$ = this.customName.select(selector)
}`,
{
code: `
import { Store, select } from '@ngrx/store'
import { inject } from '@angular/core'
class Ok16 {
private store = inject(Store)
foo$ = this.store.pipe(select(selector))
}`,
options: [SelectStyle.Operator],
},
{
code: `
import { select, Store } from '@ngrx/store'
import { inject } from '@angular/core'
class Ok17 {
private store = inject(Store)
foo$ = this.store.select(selector)
}`,
options: [SelectStyle.Method],
},
];

const invalidConstructor: () => RunTests['invalid'] = () => [
fromFixture(
`
import { select, Store } from '@ngrx/store'
Expand Down Expand Up @@ -265,7 +337,174 @@ class NotOk5 {
),
];

const invalidInject: () => RunTests['invalid'] = () => [
fromFixture(
`
import { select, Store } from '@ngrx/store'
~~~~~~ [${SelectStyle.Method}]
import { inject } from '@angular/core'
class NotOk6 {
private store = inject(Store)
foo$ = this.store.pipe( select(selector), )
~~~~~~ [${SelectStyle.Method}]
}`,
{
output: `
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class NotOk6 {
private store = inject(Store)
foo$ = this.store. select((selector), )
}`,
}
),
fromFixture(
`
import { Store, select } from '@ngrx/store'
~~~~~~ [${SelectStyle.Method}]
import { inject } from '@angular/core'
class NotOk7 {
private store = inject(Store)
foo$ = this.store.pipe (select
~~~~~~ [${SelectStyle.Method}]
(selector, selector2), filter(Boolean))
}`,
{
options: [SelectStyle.Method],
output: `
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class NotOk7 {
private store = inject(Store)
foo$ = this.store.select
(selector, selector2).pipe ( filter(Boolean))
}`,
}
),
fromFixture(
`
import { select, Store } from '@ngrx/store'
import { inject } from '@angular/core'
class NotOk8 {
private store = inject(Store)
private customStore = inject(Store)
foo$ = this.store.select(
~~~~~~ [${SelectStyle.Operator}]
selector,
)
bar$: Observable<unknown>
ngOnInit() {
this.bar$ = this.customStore.select(
~~~~~~ [${SelectStyle.Operator}]
selector,
)
}
}`,
{
options: [SelectStyle.Operator],
output: `
import { select, Store } from '@ngrx/store'
import { inject } from '@angular/core'
class NotOk8 {
private store = inject(Store)
private customStore = inject(Store)
foo$ = this.store.pipe(select(
selector,
))
bar$: Observable<unknown>
ngOnInit() {
this.bar$ = this.customStore.pipe(select(
selector,
))
}
}`,
}
),
fromFixture(
`
import {
Store,
select,
~~~~~~ [${SelectStyle.Method}]
} from '@ngrx/store'
import { inject } from '@angular/core'
class NotOk9 {
private store = inject(Store)
foo$ = this.store.pipe(select(selector), map(toItem)).pipe()
~~~~~~ [${SelectStyle.Method}]
bar$ = this.store.
select(selector).pipe()
baz$ = this.store.pipe(
select(({ customers }) => customers), map(toItem),
~~~~~~ [${SelectStyle.Method}]
).pipe()
}
class NotOk10 {
private readonly store = inject(Store)
foo$ = this.store.pipe(select(selector), map(toItem)).pipe()
~~~~~~ [${SelectStyle.Method}]
}`,
{
options: [SelectStyle.Method],
output: `
import {
Store,
} from '@ngrx/store'
import { inject } from '@angular/core'
class NotOk9 {
private store = inject(Store)
foo$ = this.store.select(selector).pipe( map(toItem)).pipe()
bar$ = this.store.
select(selector).pipe()
baz$ = this.store.select(({ customers }) => customers).pipe(
map(toItem),
).pipe()
}
class NotOk10 {
private readonly store = inject(Store)
foo$ = this.store.select(selector).pipe( map(toItem)).pipe()
}`,
}
),
fromFixture(
`
import type {Creator} from '@ngrx/store'
import { Store } from '@ngrx/store'
import { inject } from '@angular/core'
class NotOk11 {
private store = inject(Store)
foo$ = this.store.select(selector)
~~~~~~ [${SelectStyle.Operator}]
}`,
{
options: [SelectStyle.Operator],
output: `
import type {Creator} from '@ngrx/store'
import { Store, select } from '@ngrx/store'
import { inject } from '@angular/core'
class NotOk11 {
private store = inject(Store)
foo$ = this.store.pipe(select(selector))
}`,
}
),
];

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

0 comments on commit cbc9b20

Please sign in to comment.