Skip to content

Commit

Permalink
Added remaining tests for Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
marziply committed Aug 30, 2021
1 parent 38589fa commit 02e586f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
13 changes: 4 additions & 9 deletions src/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ export default class Filter {

this.filter = filter
this.key = key
this.params = params
this.name = name
this.params = params
this.fn = filters[this.name]
this.flag = new Flag(flag)

if (!this.fn) throw new FilterNotFoundError(name)
}

apply (node, data) {
Expand All @@ -51,14 +54,6 @@ export default class Filter {
})
}

get fn () {
const fn = filters[this.name]

if (fn) return fn

throw new FilterNotFoundError(this.name)
}

get args () {
return this.params
?.replace(/\(([\w$_,]*)\)/g, '$1')
Expand Down
38 changes: 35 additions & 3 deletions tests/filter.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { jest } from '@jest/globals'
import { filters } from '../src/filter.js'
import Filter, { filters } from '../src/filter.js'
import Flag from '../src/flag.js'
import yank from '../src/yank.js'
import data from './data.json'

Expand Down Expand Up @@ -192,8 +193,39 @@ describe('src/filter', () => {
})

describe('Filter', () => {
describe('filter', () => {
// @TODO
describe('constructor', () => {
it('should set filter, key, params, name, and flag', () => {
const str = 'as(foo)'
const filter = new Filter(str)

expect(filter.filter).toEqual(str)
expect(filter.key).toEqual('as')
expect(filter.name).toEqual('as')
expect(filter.params).toEqual('(foo)')
expect(filter.flag).toBeInstanceOf(Flag)
})
})

describe('apply', () => {
it('should run fn() with options', () => {
const filter = new Filter('as(foo)')
const node = {
value: 'test-node'
}
const data = {
value: 'test-data'
}

filter.fn = jest.fn()
filter.apply(node, data)

expect(filter.fn).toBeCalledWith({
flag: filter.flag,
args: filter.args,
node,
data
})
})
})
})
})

0 comments on commit 02e586f

Please sign in to comment.