import { describe , it , afterEach } from 'mocha' ; import assert from 'assert' ; import sinon from 'sinon' ; import nock from 'nock' ; import { resolve } from 'path' ; import { Selectors , loadSelectors , ObjectSelector } from '../src/object-selector' ; const sandbox = sinon . createSandbox ( ) ; nock . disableNetConnect ( ) ; const fixturesPath = resolve ( __dirname , '../../test/fixtures' ) ; interface TestObject { name : string ; id : number ; evil : boolean ; children ? : TestObject [ ] ; } describe ( 'object-selector' , ( ) => { afterEach ( ( ) => { sandbox . restore ( ) ; nock . cleanAll ( ) ; } ) ; describe ( 'loadSelectors' , ( ) => { const objs = require ( resolve ( fixturesPath , './objs' ) ) ; it ( 'throws an error with a wrong format' , ( ) => { assert . throws ( ( ) => loadSelectors ( 'test/fixtures/wrong.yaml' ) , /must be array/ ) ; } ) ; it ( 'loads Selectors correctly' , ( ) => { const selectors = loadSelectors ( 'test/fixtures/correct.yaml' ) ; assert . strictEqual ( selectors . length , 1 ) ; assert . strictEqual ( selectors [ 0 ] . length , 3 ) ; assert . strictEqual ( selectors [ 0 ] [ 0 ] , 'name' ) ; assert . strictEqual ( selectors [ 0 ] [ 1 ] , 'eq' ) ; assert . strictEqual ( selectors [ 0 ] [ 2 ] , 'test' ) ; } ) ; it ( 'select collect objects with "eq" operator' , ( ) => { const selectors = loadSelectors ( 'test/fixtures/correct.yaml' ) ; const objSelector = new ObjectSelector < TestObject > ( [ selectors ] ) ; const selected = objSelector . select ( objs ) ; objectAssert ( selected , [ 'test' ] ) ; } ) ; it ( 'select collect objects with "ne" operator' , ( ) => { const selectors = [ [ 'evil' , 'eq' , true ] , [ 'name' , 'ne' , 'evil' ] , ] ; const objSelector = new ObjectSelector < TestObject > ( [ selectors ] ) ; const selected = objSelector . select ( objs ) ; objectAssert ( selected , [ 'name2' ] ) ; } ) ; it ( 'select collect objects with multipel Selectors combined with OR' , ( ) => { const selectors1 = [ [ 'name' , 'eq' , 'test' ] ] ; const selectors2 = [ [ 'name' , 'eq' , 'evil' ] ] ; const objSelector = new ObjectSelector < TestObject > ( [ selectors1 , selectors2 , ] ) ; const selected = objSelector . select ( objs ) ; objectAssert ( selected , [ 'test' , 'evil' ] ) ; } ) ; it ( 'select collect objects with a regex' , ( ) => { const selectors = [ [ 'name' , 'regex' , '(test|hasachild)' ] ] ; const objSelector = new ObjectSelector < TestObject > ( [ selectors ] ) ; const selected = objSelector . select ( objs ) ; objectAssert ( selected , [ 'test' , 'hasachild' ] ) ; } ) ; it ( 'select collect objects with an array index' , ( ) => { const selectors = [ [ 'children.0.name' , 'regex' , 'child' ] ] ; const objSelector = new ObjectSelector < TestObject > ( [ selectors ] ) ; const selected = objSelector . select ( objs ) ; objectAssert ( selected , [ 'hasachild' , 'hasagrandchild' ] ) ; } ) ; it ( 'select collect objects with "anyof" operator' , ( ) => { const selectors = [ [ 'name' , 'anyof' , [ 'test' , 'name2' ] ] ] ; const objSelector = new ObjectSelector < TestObject > ( [ selectors ] ) ; const selected = objSelector . select ( objs ) ; objectAssert ( selected , [ 'test' , 'name2' ] ) ; } ) ; it ( 'select collect objects with nested property' , ( ) => { const selectors = [ [ 'children.0.children.0.id' , 'eq' , 8 ] ] ; const objSelector = new ObjectSelector < TestObject > ( [ selectors ] ) ; const selected = objSelector . select ( objs ) ; objectAssert ( selected , [ 'hasagrandchild' ] ) ; } ) ; it ( 'returns nothing with empty selectors' , ( ) => { const selectors = [ ] ; const objSelector = new ObjectSelector < TestObject > ( [ selectors ] ) ; const selected = objSelector . select ( objs ) ; assert . strictEqual ( selected . length , 0 ) ; } ) ; } ) ; } ) ; function objectAssert ( selected , expected ) { assert . strictEqual ( expected . length , selected . length ) ; assert . ok ( selected . every ( ( obj ) => { return expected . includes ( obj . name ) ; } ) ) ; }