index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict'
  2. const assert = require( 'assert' )
  3. const data = require( './data' )
  4. const implementation = require( './implementation' )
  5. const baseAdapter = require( '../' )
  6. const adapter = baseAdapter( implementation )
  7. const getById = id => adapter.findOne(
  8. node => adapter.getAttributeValue( node, 'id' ) === id,
  9. data
  10. )
  11. const getByName = name => adapter.findAll(
  12. node => adapter.getName( node ) === name,
  13. data
  14. )
  15. const getByClass = className => adapter.findAll(
  16. node => adapter.getAttributeValue( node, 'class' ) === className,
  17. data
  18. )
  19. const existsName = name => adapter.existsOne(
  20. node => adapter.getName( node ) === name,
  21. data
  22. )
  23. const container = getById( 'container' )
  24. const strong = getByName( 'strong' )[ 0 ]
  25. const hello = strong.children[ 0 ]
  26. const world = container.children[ 1 ]
  27. describe( 'css-select-base-adapter', () => {
  28. it( 'getAttributeValue', () => {
  29. assert( container )
  30. })
  31. it( 'getName', () => {
  32. assert( strong )
  33. })
  34. it( 'findOne', () => {
  35. assert( container )
  36. })
  37. it( 'findAll', () => {
  38. const messages = getByClass( 'message' )
  39. assert.equal( messages.length, 2 )
  40. assert.equal( messages[ 0 ], container )
  41. assert.equal( messages[ 1 ], strong )
  42. })
  43. it( 'getParent', () => {
  44. const parent = adapter.getParent( strong )
  45. assert.equal( parent, container )
  46. })
  47. it( 'getSiblings', () => {
  48. const siblings = adapter.getSiblings( strong )
  49. assert.equal( siblings[ 0 ], strong )
  50. assert.equal( siblings[ 1 ], world )
  51. })
  52. it( 'getChildren', () => {
  53. const children = adapter.getChildren( container )
  54. assert.equal( children[ 0 ], strong )
  55. })
  56. it( 'getText', () => {
  57. const text = adapter.getText( container )
  58. assert.equal( text, 'Hello, World!' )
  59. })
  60. it( 'isTag', () => {
  61. assert( adapter.isTag( container ) )
  62. assert( adapter.isTag( strong ) )
  63. assert( !adapter.isTag( hello ) )
  64. })
  65. it( 'hasAttrib', () => {
  66. assert( adapter.hasAttrib( container, 'id' ) )
  67. assert( !adapter.hasAttrib( strong, 'id' ) )
  68. })
  69. it( 'existsOne', () => {
  70. assert( existsName( 'strong' ) )
  71. assert( !existsName( 'blink' ) )
  72. })
  73. it( 'removeSubsets', () => {
  74. const removed = adapter.removeSubsets([ container, strong, container ])
  75. assert.equal( removed.length, 1 )
  76. assert.equal( removed[ 0 ], container )
  77. })
  78. })