import 'mocha' ; import { expect } from 'chai' ; import { UserSelection } from '@/application/Context/State/Selection/UserSelection' ; import { ApplicationCode } from '@/application/Context/State/Code/ApplicationCode' ; import { CategoryCollectionState } from '@/application/Context/State/CategoryCollectionState' ; import { OperatingSystem } from '@/domain/OperatingSystem' ; import { IScript } from '@/domain/IScript' ; import { ScriptStub } from '@tests/unit/stubs/ScriptStub' ; import { CategoryStub } from '@tests/unit/stubs/CategoryStub' ; import { CategoryCollectionStub } from '@tests/unit/stubs/CategoryCollectionStub' ; describe ( 'CategoryCollectionState' , ( ) => { describe ( 'code' , ( ) => { it ( 'initialized with empty code' , ( ) => { const collection = new CategoryCollectionStub ( ) ; const sut = new CategoryCollectionState ( collection ) ; const code = sut . code . current ; expect ( ! code ) ; } ) ; it ( 'reacts to selection changes as expected' , ( ) => { const collection = new CategoryCollectionStub ( ) . withAction ( new CategoryStub ( 0 ) . withScriptIds ( 'scriptId' ) ) ; const selectionStub = new UserSelection ( collection , [ ] ) ; const expectedCodeGenerator = new ApplicationCode ( selectionStub , collection . scripting ) ; selectionStub . selectAll ( ) ; const expectedCode = expectedCodeGenerator . current ; const sut = new CategoryCollectionState ( collection ) ; sut . selection . selectAll ( ) ; const actualCode = sut . code . current ; expect ( actualCode ) . to . equal ( expectedCode ) ; } ) ; } ) ; describe ( 'os' , ( ) => { it ( 'same as its collection' , ( ) => { const expected = OperatingSystem . macOS ; const collection = new CategoryCollectionStub ( ) . withOs ( expected ) ; const sut = new CategoryCollectionState ( collection ) ; const actual = sut . os ; expect ( expected ) . to . equal ( actual ) ; } ) ; } ) ; describe ( 'selection' , ( ) => { it ( 'initialized with no selection' , ( ) => { const collection = new CategoryCollectionStub ( ) ; const sut = new CategoryCollectionState ( collection ) ; const actual = sut . selection . selectedScripts . length ; expect ( actual ) . to . equal ( 0 ) ; } ) ; it ( 'can select a script from current collection' , ( ) => { const expectedScript = new ScriptStub ( 'scriptId' ) ; const collection = new CategoryCollectionStub ( ) . withAction ( new CategoryStub ( 0 ) . withScript ( expectedScript ) ) ; const sut = new CategoryCollectionState ( collection ) ; sut . selection . selectAll ( ) ; expect ( sut . selection . selectedScripts . length ) . to . equal ( 1 ) ; expect ( sut . selection . isSelected ( expectedScript . id ) ) . to . equal ( true ) ; } ) ; } ) ; describe ( 'filter' , ( ) => { it ( 'initialized with an empty filter' , ( ) => { const collection = new CategoryCollectionStub ( ) ; const sut = new CategoryCollectionState ( collection ) ; const actual = sut . filter . currentFilter ; expect ( actual ) . to . equal ( undefined ) ; } ) ; it ( 'can match a script from current collection' , ( ) => { const scriptNameFilter = 'scriptName' ; const expectedScript = new ScriptStub ( 'scriptId' ) . withName ( scriptNameFilter ) ; const collection = new CategoryCollectionStub ( ) . withAction ( new CategoryStub ( 0 ) . withScript ( expectedScript ) ) ; const sut = new CategoryCollectionState ( collection ) ; let actualScript ; sut . filter . filtered . on ( ( result ) => { [ actualScript ] = result . scriptMatches ; } ) ; sut . filter . setFilter ( scriptNameFilter ) ; expect ( expectedScript ) . to . equal ( actualScript ) ; } ) ; } ) ; } ) ;