checkFeatureMatchesStyleRule.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import { Feature } from 'ol';
  2. import { ComparisonOperation } from '../types';
  3. import { checkFeatureMatchesStyleRule } from './checkFeatureMatchesStyleRule';
  4. describe('check if feature matches style rule', () => {
  5. it('can compare with numbers', () => {
  6. const feature = new Feature({
  7. number: 3,
  8. });
  9. expect(
  10. checkFeatureMatchesStyleRule(
  11. {
  12. operation: ComparisonOperation.EQ,
  13. property: 'number',
  14. value: 3,
  15. },
  16. feature
  17. )
  18. ).toEqual(true);
  19. expect(
  20. checkFeatureMatchesStyleRule(
  21. {
  22. operation: ComparisonOperation.EQ,
  23. property: 'number',
  24. value: '3',
  25. },
  26. feature
  27. )
  28. ).toEqual(true);
  29. expect(
  30. checkFeatureMatchesStyleRule(
  31. {
  32. operation: ComparisonOperation.LT,
  33. property: 'number',
  34. value: 2,
  35. },
  36. feature
  37. )
  38. ).toEqual(false);
  39. expect(
  40. checkFeatureMatchesStyleRule(
  41. {
  42. operation: ComparisonOperation.LTE,
  43. property: 'number',
  44. value: 3,
  45. },
  46. feature
  47. )
  48. ).toEqual(true);
  49. expect(
  50. checkFeatureMatchesStyleRule(
  51. {
  52. operation: ComparisonOperation.GT,
  53. property: 'number',
  54. value: 3,
  55. },
  56. feature
  57. )
  58. ).toEqual(false);
  59. expect(
  60. checkFeatureMatchesStyleRule(
  61. {
  62. operation: ComparisonOperation.GTE,
  63. property: 'number',
  64. value: 3,
  65. },
  66. feature
  67. )
  68. ).toEqual(true);
  69. expect(
  70. checkFeatureMatchesStyleRule(
  71. {
  72. operation: ComparisonOperation.NEQ,
  73. property: 'number',
  74. value: 3,
  75. },
  76. feature
  77. )
  78. ).toEqual(false);
  79. });
  80. it('can compare with strings', () => {
  81. const feature = new Feature({
  82. string: 'b',
  83. });
  84. expect(
  85. checkFeatureMatchesStyleRule(
  86. {
  87. operation: ComparisonOperation.EQ,
  88. property: 'string',
  89. value: 'B',
  90. },
  91. feature
  92. )
  93. ).toEqual(false);
  94. expect(
  95. checkFeatureMatchesStyleRule(
  96. {
  97. operation: ComparisonOperation.LT,
  98. property: 'string',
  99. value: 'c',
  100. },
  101. feature
  102. )
  103. ).toEqual(true);
  104. expect(
  105. checkFeatureMatchesStyleRule(
  106. {
  107. operation: ComparisonOperation.LTE,
  108. property: 'string',
  109. value: 'bc',
  110. },
  111. feature
  112. )
  113. ).toEqual(true);
  114. expect(
  115. checkFeatureMatchesStyleRule(
  116. {
  117. operation: ComparisonOperation.GT,
  118. property: 'string',
  119. value: 'ab',
  120. },
  121. feature
  122. )
  123. ).toEqual(true);
  124. expect(
  125. checkFeatureMatchesStyleRule(
  126. {
  127. operation: ComparisonOperation.GTE,
  128. property: 'string',
  129. value: 'abc',
  130. },
  131. feature
  132. )
  133. ).toEqual(true);
  134. expect(
  135. checkFeatureMatchesStyleRule(
  136. {
  137. operation: ComparisonOperation.NEQ,
  138. property: 'string',
  139. value: 'b',
  140. },
  141. feature
  142. )
  143. ).toEqual(false);
  144. });
  145. it('can compare with booleans', () => {
  146. const feature = new Feature({
  147. name: 'test polygon',
  148. });
  149. feature.setProperties({ boolean: false });
  150. expect(
  151. checkFeatureMatchesStyleRule(
  152. {
  153. operation: ComparisonOperation.EQ,
  154. property: 'boolean',
  155. value: false,
  156. },
  157. feature
  158. )
  159. ).toEqual(true);
  160. expect(
  161. checkFeatureMatchesStyleRule(
  162. {
  163. operation: ComparisonOperation.LT,
  164. property: 'boolean',
  165. value: true,
  166. },
  167. feature
  168. )
  169. ).toEqual(true);
  170. expect(
  171. checkFeatureMatchesStyleRule(
  172. {
  173. operation: ComparisonOperation.LTE,
  174. property: 'boolean',
  175. value: true,
  176. },
  177. feature
  178. )
  179. ).toEqual(true);
  180. expect(
  181. checkFeatureMatchesStyleRule(
  182. {
  183. operation: ComparisonOperation.GT,
  184. property: 'boolean',
  185. value: false,
  186. },
  187. feature
  188. )
  189. ).toEqual(false);
  190. expect(
  191. checkFeatureMatchesStyleRule(
  192. {
  193. operation: ComparisonOperation.GTE,
  194. property: 'boolean',
  195. value: false,
  196. },
  197. feature
  198. )
  199. ).toEqual(true);
  200. expect(
  201. checkFeatureMatchesStyleRule(
  202. {
  203. operation: ComparisonOperation.NEQ,
  204. property: 'boolean',
  205. value: false,
  206. },
  207. feature
  208. )
  209. ).toEqual(false);
  210. });
  211. });