import 'mocha' ; import { expect } from 'chai' ; import { IFilterResult } from '@/application/Context/State/Filter/IFilterResult' ; import { UserFilter } from '@/application/Context/State/Filter/UserFilter' ; import { CategoryStub } from '@tests/unit/stubs/CategoryStub' ; import { ScriptStub } from '@tests/unit/stubs/ScriptStub' ; import { CategoryCollectionStub } from '@tests/unit/stubs/CategoryCollectionStub' ; describe ( 'UserFilter' , ( ) => { describe ( 'removeFilter' , ( ) => { it ( 'signals when removing filter' , ( ) => { let isCalled = false ; const sut = new UserFilter ( new CategoryCollectionStub ( ) ) ; sut . filterRemoved . on ( ( ) => { isCalled = true ; } ) ; sut . removeFilter ( ) ; expect ( isCalled ) . to . be . equal ( true ) ; } ) ; it ( 'sets currentFilter to undefined' , ( ) => { const sut = new UserFilter ( new CategoryCollectionStub ( ) ) ; sut . setFilter ( 'non-important' ) ; sut . removeFilter ( ) ; expect ( sut . currentFilter ) . to . be . equal ( undefined ) ; } ) ; } ) ; describe ( 'setFilter' , ( ) => { it ( 'signals when no matches' , ( ) => { let actual ; const nonMatchingFilter = 'non matching filter' ; const sut = new UserFilter ( new CategoryCollectionStub ( ) ) ; sut . filtered . on ( ( filterResult ) => { actual = filterResult ; } ) ; sut . setFilter ( nonMatchingFilter ) ; expect ( actual . hasAnyMatches ( ) ) . be . equal ( false ) ; expect ( actual . query ) . to . equal ( nonMatchingFilter ) ; } ) ; it ( 'sets currentFilter as expected when no matches' , ( ) => { const nonMatchingFilter = 'non matching filter' ; const sut = new UserFilter ( new CategoryCollectionStub ( ) ) ; sut . setFilter ( nonMatchingFilter ) ; const actual = sut . currentFilter ; expect ( actual . hasAnyMatches ( ) ) . be . equal ( false ) ; expect ( actual . query ) . to . equal ( nonMatchingFilter ) ; } ) ; describe ( 'signals when matches' , ( ) => { describe ( 'signals when script matches' , ( ) => { it ( 'code matches' , ( ) => { const code = 'HELLO world' ; const filter = 'Hello WoRLD' ; let actual ; const script = new ScriptStub ( 'id' ) . withCode ( code ) ; const category = new CategoryStub ( 33 ) . withScript ( script ) ; const sut = new UserFilter ( new CategoryCollectionStub ( ) . withAction ( category ) ) ; sut . filtered . on ( ( filterResult ) => { actual = filterResult ; } ) ; sut . setFilter ( filter ) ; expect ( actual . hasAnyMatches ( ) ) . be . equal ( true ) ; expect ( actual . categoryMatches ) . to . have . lengthOf ( 0 ) ; expect ( actual . scriptMatches ) . to . have . lengthOf ( 1 ) ; expect ( actual . scriptMatches [ 0 ] ) . to . deep . equal ( script ) ; expect ( actual . query ) . to . equal ( filter ) ; expect ( sut . currentFilter ) . to . deep . equal ( actual ) ; } ) ; it ( 'revertCode matches' , ( ) => { const revertCode = 'HELLO world' ; const filter = 'Hello WoRLD' ; let actual ; const script = new ScriptStub ( 'id' ) . withRevertCode ( revertCode ) ; const category = new CategoryStub ( 33 ) . withScript ( script ) ; const sut = new UserFilter ( new CategoryCollectionStub ( ) . withAction ( category ) ) ; sut . filtered . on ( ( filterResult ) => { actual = filterResult ; } ) ; sut . setFilter ( filter ) ; expect ( actual . hasAnyMatches ( ) ) . be . equal ( true ) ; expect ( actual . categoryMatches ) . to . have . lengthOf ( 0 ) ; expect ( actual . scriptMatches ) . to . have . lengthOf ( 1 ) ; expect ( actual . scriptMatches [ 0 ] ) . to . deep . equal ( script ) ; expect ( actual . query ) . to . equal ( filter ) ; expect ( sut . currentFilter ) . to . deep . equal ( actual ) ; } ) ; it ( 'name matches' , ( ) => { const name = 'HELLO world' ; const filter = 'Hello WoRLD' ; let actual ; const script = new ScriptStub ( 'id' ) . withName ( name ) ; const category = new CategoryStub ( 33 ) . withScript ( script ) ; const sut = new UserFilter ( new CategoryCollectionStub ( ) . withAction ( category ) ) ; sut . filtered . on ( ( filterResult ) => { actual = filterResult ; } ) ; sut . setFilter ( filter ) ; expect ( actual . hasAnyMatches ( ) ) . be . equal ( true ) ; expect ( actual . categoryMatches ) . to . have . lengthOf ( 0 ) ; expect ( actual . scriptMatches ) . to . have . lengthOf ( 1 ) ; expect ( actual . scriptMatches [ 0 ] ) . to . deep . equal ( script ) ; expect ( actual . query ) . to . equal ( filter ) ; expect ( sut . currentFilter ) . to . deep . equal ( actual ) ; } ) ; } ) ; it ( 'signals when category matches' , ( ) => { const categoryName = 'HELLO world' ; const filter = 'Hello WoRLD' ; let actual ; const category = new CategoryStub ( 55 ) . withName ( categoryName ) ; const sut = new UserFilter ( new CategoryCollectionStub ( ) . withAction ( category ) ) ; sut . filtered . on ( ( filterResult ) => { actual = filterResult ; } ) ; sut . setFilter ( filter ) ; expect ( actual . hasAnyMatches ( ) ) . be . equal ( true ) ; expect ( actual . categoryMatches ) . to . have . lengthOf ( 1 ) ; expect ( actual . categoryMatches [ 0 ] ) . to . deep . equal ( category ) ; expect ( actual . scriptMatches ) . to . have . lengthOf ( 0 ) ; expect ( actual . query ) . to . equal ( filter ) ; expect ( sut . currentFilter ) . to . deep . equal ( actual ) ; } ) ; it ( 'signals when category and script matches' , ( ) => { const matchingText = 'HELLO world' ; const filter = 'Hello WoRLD' ; let actual ; const script = new ScriptStub ( 'script' ) . withName ( matchingText ) ; const category = new CategoryStub ( 55 ) . withName ( matchingText ) . withScript ( script ) ; const collection = new CategoryCollectionStub ( ) . withAction ( category ) ; const sut = new UserFilter ( collection ) ; sut . filtered . on ( ( filterResult ) => { actual = filterResult ; } ) ; sut . setFilter ( filter ) ; expect ( actual . hasAnyMatches ( ) ) . be . equal ( true ) ; expect ( actual . categoryMatches ) . to . have . lengthOf ( 1 ) ; expect ( actual . categoryMatches [ 0 ] ) . to . deep . equal ( category ) ; expect ( actual . scriptMatches ) . to . have . lengthOf ( 1 ) ; expect ( actual . scriptMatches [ 0 ] ) . to . deep . equal ( script ) ; expect ( actual . query ) . to . equal ( filter ) ; expect ( sut . currentFilter ) . to . deep . equal ( actual ) ; } ) ; } ) ; } ) ; } ) ;