You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

912 rivejä
25 KiB

  1. /* eslint-env mocha */
  2. import expect from "expect"
  3. import { fromJS, OrderedMap } from "immutable"
  4. import {
  5. mapToList,
  6. validateMinLength,
  7. validateMaxLength,
  8. validateDateTime,
  9. validateGuid,
  10. validateNumber,
  11. validateInteger,
  12. validateParam,
  13. validateFile,
  14. validateMaximum,
  15. validateMinimum,
  16. fromJSOrdered,
  17. getAcceptControllingResponse,
  18. createDeepLinkPath,
  19. escapeDeepLinkPath
  20. } from "core/utils"
  21. import win from "core/window"
  22. describe("utils", function() {
  23. describe("mapToList", function(){
  24. it("should convert a map to a list, setting `key`", function(){
  25. // With
  26. const aMap = fromJS({
  27. a: {
  28. one: 1,
  29. },
  30. b: {
  31. two: 2,
  32. }
  33. })
  34. // When
  35. const aList = mapToList(aMap, "someKey")
  36. // Then
  37. expect(aList.toJS()).toEqual([
  38. { someKey: "a", one: 1 },
  39. { someKey: "b", two: 2 },
  40. ])
  41. })
  42. it("should flatten an arbitrarily deep map", function(){
  43. // With
  44. const aMap = fromJS({
  45. a: {
  46. one: {
  47. alpha: true
  48. }
  49. },
  50. b: {
  51. two: {
  52. bravo: true
  53. },
  54. three: {
  55. charlie: true
  56. }
  57. }
  58. })
  59. // When
  60. const aList = mapToList(aMap, ["levelA", "levelB"])
  61. // Then
  62. expect(aList.toJS()).toEqual([
  63. { levelA: "a", levelB: "one", alpha: true },
  64. { levelA: "b", levelB: "two", bravo: true },
  65. { levelA: "b", levelB: "three", charlie: true },
  66. ])
  67. })
  68. it("should handle an empty map", function(){
  69. // With
  70. const aMap = fromJS({})
  71. // When
  72. const aList = mapToList(aMap, ["levelA", "levelB"])
  73. // Then
  74. expect(aList.toJS()).toEqual([])
  75. })
  76. })
  77. describe("validateMaximum", function() {
  78. let errorMessage = "Value must be less than Maximum"
  79. it("doesn't return for valid input", function() {
  80. expect(validateMaximum(9, 10)).toBeFalsy()
  81. expect(validateMaximum(19, 20)).toBeFalsy()
  82. })
  83. it("returns a message for invalid input", function() {
  84. expect(validateMaximum(1, 0)).toEqual(errorMessage)
  85. expect(validateMaximum(10, 9)).toEqual(errorMessage)
  86. expect(validateMaximum(20, 19)).toEqual(errorMessage)
  87. })
  88. })
  89. describe("validateMinimum", function() {
  90. let errorMessage = "Value must be greater than Minimum"
  91. it("doesn't return for valid input", function() {
  92. expect(validateMinimum(2, 1)).toBeFalsy()
  93. expect(validateMinimum(20, 10)).toBeFalsy()
  94. })
  95. it("returns a message for invalid input", function() {
  96. expect(validateMinimum(-1, 0)).toEqual(errorMessage)
  97. expect(validateMinimum(1, 2)).toEqual(errorMessage)
  98. expect(validateMinimum(10, 20)).toEqual(errorMessage)
  99. })
  100. })
  101. describe("validateNumber", function() {
  102. let errorMessage = "Value must be a number"
  103. it("doesn't return for whole numbers", function() {
  104. expect(validateNumber(0)).toBeFalsy()
  105. expect(validateNumber(1)).toBeFalsy()
  106. expect(validateNumber(20)).toBeFalsy()
  107. expect(validateNumber(5000000)).toBeFalsy()
  108. expect(validateNumber("1")).toBeFalsy()
  109. expect(validateNumber("2")).toBeFalsy()
  110. expect(validateNumber(-1)).toBeFalsy()
  111. expect(validateNumber(-20)).toBeFalsy()
  112. expect(validateNumber(-5000000)).toBeFalsy()
  113. })
  114. it("doesn't return for negative numbers", function() {
  115. expect(validateNumber(-1)).toBeFalsy()
  116. expect(validateNumber(-20)).toBeFalsy()
  117. expect(validateNumber(-5000000)).toBeFalsy()
  118. })
  119. it("doesn't return for decimal numbers", function() {
  120. expect(validateNumber(1.1)).toBeFalsy()
  121. expect(validateNumber(2.5)).toBeFalsy()
  122. expect(validateNumber(-30.99)).toBeFalsy()
  123. })
  124. it("returns a message for strings", function() {
  125. expect(validateNumber("")).toEqual(errorMessage)
  126. expect(validateNumber(" ")).toEqual(errorMessage)
  127. expect(validateNumber("test")).toEqual(errorMessage)
  128. })
  129. it("returns a message for invalid input", function() {
  130. expect(validateNumber(undefined)).toEqual(errorMessage)
  131. expect(validateNumber(null)).toEqual(errorMessage)
  132. expect(validateNumber({})).toEqual(errorMessage)
  133. expect(validateNumber([])).toEqual(errorMessage)
  134. expect(validateNumber(true)).toEqual(errorMessage)
  135. expect(validateNumber(false)).toEqual(errorMessage)
  136. })
  137. })
  138. describe("validateInteger", function() {
  139. let errorMessage = "Value must be an integer"
  140. it("doesn't return for positive integers", function() {
  141. expect(validateInteger(0)).toBeFalsy()
  142. expect(validateInteger(1)).toBeFalsy()
  143. expect(validateInteger(20)).toBeFalsy()
  144. expect(validateInteger(5000000)).toBeFalsy()
  145. expect(validateInteger("1")).toBeFalsy()
  146. expect(validateInteger("2")).toBeFalsy()
  147. expect(validateInteger(-1)).toBeFalsy()
  148. expect(validateInteger(-20)).toBeFalsy()
  149. expect(validateInteger(-5000000)).toBeFalsy()
  150. })
  151. it("doesn't return for negative integers", function() {
  152. expect(validateInteger(-1)).toBeFalsy()
  153. expect(validateInteger(-20)).toBeFalsy()
  154. expect(validateInteger(-5000000)).toBeFalsy()
  155. })
  156. it("returns a message for decimal values", function() {
  157. expect(validateInteger(1.1)).toEqual(errorMessage)
  158. expect(validateInteger(2.5)).toEqual(errorMessage)
  159. expect(validateInteger(-30.99)).toEqual(errorMessage)
  160. })
  161. it("returns a message for strings", function() {
  162. expect(validateInteger("")).toEqual(errorMessage)
  163. expect(validateInteger(" ")).toEqual(errorMessage)
  164. expect(validateInteger("test")).toEqual(errorMessage)
  165. })
  166. it("returns a message for invalid input", function() {
  167. expect(validateInteger(undefined)).toEqual(errorMessage)
  168. expect(validateInteger(null)).toEqual(errorMessage)
  169. expect(validateInteger({})).toEqual(errorMessage)
  170. expect(validateInteger([])).toEqual(errorMessage)
  171. expect(validateInteger(true)).toEqual(errorMessage)
  172. expect(validateInteger(false)).toEqual(errorMessage)
  173. })
  174. })
  175. describe("validateFile", function() {
  176. let errorMessage = "Value must be a file"
  177. it("validates against objects which are instances of 'File'", function() {
  178. let fileObj = new win.File([], "Test File")
  179. expect(validateFile(fileObj)).toBeFalsy()
  180. expect(validateFile(null)).toBeFalsy()
  181. expect(validateFile(undefined)).toBeFalsy()
  182. expect(validateFile(1)).toEqual(errorMessage)
  183. expect(validateFile("string")).toEqual(errorMessage)
  184. })
  185. })
  186. describe("validateDateTime", function() {
  187. let errorMessage = "Value must be a DateTime"
  188. it("doesn't return for valid dates", function() {
  189. expect(validateDateTime("Mon, 25 Dec 1995 13:30:00 +0430")).toBeFalsy()
  190. })
  191. it("returns a message for invalid input'", function() {
  192. expect(validateDateTime(null)).toEqual(errorMessage)
  193. expect(validateDateTime("string")).toEqual(errorMessage)
  194. })
  195. })
  196. describe("validateGuid", function() {
  197. let errorMessage = "Value must be a Guid"
  198. it("doesn't return for valid guid", function() {
  199. expect(validateGuid("8ce4811e-cec5-4a29-891a-15d1917153c1")).toBeFalsy()
  200. expect(validateGuid("{8ce4811e-cec5-4a29-891a-15d1917153c1}")).toBeFalsy()
  201. })
  202. it("returns a message for invalid input'", function() {
  203. expect(validateGuid(1)).toEqual(errorMessage)
  204. expect(validateGuid("string")).toEqual(errorMessage)
  205. })
  206. })
  207. describe("validateMaxLength", function() {
  208. let errorMessage = "Value must be less than MaxLength"
  209. it("doesn't return for valid guid", function() {
  210. expect(validateMaxLength("a", 1)).toBeFalsy()
  211. expect(validateMaxLength("abc", 5)).toBeFalsy()
  212. })
  213. it("returns a message for invalid input'", function() {
  214. expect(validateMaxLength("abc", 0)).toEqual(errorMessage)
  215. expect(validateMaxLength("abc", 1)).toEqual(errorMessage)
  216. expect(validateMaxLength("abc", 2)).toEqual(errorMessage)
  217. })
  218. })
  219. describe("validateMinLength", function() {
  220. let errorMessage = "Value must be greater than MinLength"
  221. it("doesn't return for valid guid", function() {
  222. expect(validateMinLength("a", 1)).toBeFalsy()
  223. expect(validateMinLength("abc", 2)).toBeFalsy()
  224. })
  225. it("returns a message for invalid input'", function() {
  226. expect(validateMinLength("abc", 5)).toEqual(errorMessage)
  227. expect(validateMinLength("abc", 8)).toEqual(errorMessage)
  228. })
  229. })
  230. describe("validateParam", function() {
  231. let param = null
  232. let result = null
  233. it("skips validation when `type` is not specified", function() {
  234. // invalid type
  235. param = fromJS({
  236. required: false,
  237. type: undefined,
  238. value: ""
  239. })
  240. result = validateParam( param, false )
  241. expect( result ).toEqual( [] )
  242. })
  243. it("validates required strings", function() {
  244. // invalid string
  245. param = fromJS({
  246. required: true,
  247. type: "string",
  248. value: ""
  249. })
  250. result = validateParam( param, false )
  251. expect( result ).toEqual( ["Required field is not provided"] )
  252. // valid string
  253. param = fromJS({
  254. required: true,
  255. type: "string",
  256. value: "test string"
  257. })
  258. result = validateParam( param, false )
  259. expect( result ).toEqual( [] )
  260. // valid string with min and max length
  261. param = fromJS({
  262. required: true,
  263. type: "string",
  264. value: "test string",
  265. maxLength: 50,
  266. minLength: 1
  267. })
  268. result = validateParam( param, false )
  269. expect( result ).toEqual( [] )
  270. })
  271. it("validates required strings with min and max length", function() {
  272. // invalid string with max length
  273. param = fromJS({
  274. required: true,
  275. type: "string",
  276. value: "test string",
  277. maxLength: 5
  278. })
  279. result = validateParam( param, false )
  280. expect( result ).toEqual( ["Value must be less than MaxLength"] )
  281. // invalid string with max length 0
  282. param = fromJS({
  283. required: true,
  284. type: "string",
  285. value: "test string",
  286. maxLength: 0
  287. })
  288. result = validateParam( param, false )
  289. expect( result ).toEqual( ["Value must be less than MaxLength"] )
  290. // invalid string with min length
  291. param = fromJS({
  292. required: true,
  293. type: "string",
  294. value: "test string",
  295. minLength: 50
  296. })
  297. result = validateParam( param, false )
  298. expect( result ).toEqual( ["Value must be greater than MinLength"] )
  299. })
  300. it("validates optional strings", function() {
  301. // valid (empty) string
  302. param = fromJS({
  303. required: false,
  304. type: "string",
  305. value: ""
  306. })
  307. result = validateParam( param, false )
  308. expect( result ).toEqual( [] )
  309. // valid string
  310. param = fromJS({
  311. required: false,
  312. type: "string",
  313. value: "test"
  314. })
  315. result = validateParam( param, false )
  316. expect( result ).toEqual( [] )
  317. })
  318. it("validates required files", function() {
  319. // invalid file
  320. param = fromJS({
  321. required: true,
  322. type: "file",
  323. value: undefined
  324. })
  325. result = validateParam( param, false )
  326. expect( result ).toEqual( ["Required field is not provided"] )
  327. // valid file
  328. param = fromJS({
  329. required: true,
  330. type: "file",
  331. value: new win.File()
  332. })
  333. result = validateParam( param, false )
  334. expect( result ).toEqual( [] )
  335. })
  336. it("validates optional files", function() {
  337. // invalid file
  338. param = fromJS({
  339. required: false,
  340. type: "file",
  341. value: "not a file"
  342. })
  343. result = validateParam( param, false )
  344. expect( result ).toEqual( ["Value must be a file"] )
  345. // valid (empty) file
  346. param = fromJS({
  347. required: false,
  348. type: "file",
  349. value: undefined
  350. })
  351. result = validateParam( param, false )
  352. expect( result ).toEqual( [] )
  353. // valid file
  354. param = fromJS({
  355. required: false,
  356. type: "file",
  357. value: new win.File()
  358. })
  359. result = validateParam( param, false )
  360. expect( result ).toEqual( [] )
  361. })
  362. it("validates required arrays", function() {
  363. // invalid (empty) array
  364. param = fromJS({
  365. required: true,
  366. type: "array",
  367. value: []
  368. })
  369. result = validateParam( param, false )
  370. expect( result ).toEqual( ["Required field is not provided"] )
  371. // invalid (not an array)
  372. param = fromJS({
  373. required: true,
  374. type: "array",
  375. value: undefined
  376. })
  377. result = validateParam( param, false )
  378. expect( result ).toEqual( ["Required field is not provided"] )
  379. // invalid array, items do not match correct type
  380. param = fromJS({
  381. required: true,
  382. type: "array",
  383. value: [1],
  384. items: {
  385. type: "string"
  386. }
  387. })
  388. result = validateParam( param, false )
  389. expect( result ).toEqual( [{index: 0, error: "Value must be a string"}] )
  390. // valid array, with no 'type' for items
  391. param = fromJS({
  392. required: true,
  393. type: "array",
  394. value: ["1"]
  395. })
  396. result = validateParam( param, false )
  397. expect( result ).toEqual( [] )
  398. // valid array, items match type
  399. param = fromJS({
  400. required: true,
  401. type: "array",
  402. value: ["1"],
  403. items: {
  404. type: "string"
  405. }
  406. })
  407. result = validateParam( param, false )
  408. expect( result ).toEqual( [] )
  409. })
  410. it("validates optional arrays", function() {
  411. // valid, empty array
  412. param = fromJS({
  413. required: false,
  414. type: "array",
  415. value: []
  416. })
  417. result = validateParam( param, false )
  418. expect( result ).toEqual( [] )
  419. // invalid, items do not match correct type
  420. param = fromJS({
  421. required: false,
  422. type: "array",
  423. value: ["number"],
  424. items: {
  425. type: "number"
  426. }
  427. })
  428. result = validateParam( param, false )
  429. expect( result ).toEqual( [{index: 0, error: "Value must be a number"}] )
  430. // valid
  431. param = fromJS({
  432. required: false,
  433. type: "array",
  434. value: ["test"],
  435. items: {
  436. type: "string"
  437. }
  438. })
  439. result = validateParam( param, false )
  440. expect( result ).toEqual( [] )
  441. })
  442. it("validates required booleans", function() {
  443. // invalid boolean value
  444. param = fromJS({
  445. required: true,
  446. type: "boolean",
  447. value: undefined
  448. })
  449. result = validateParam( param, false )
  450. expect( result ).toEqual( ["Required field is not provided"] )
  451. // invalid boolean value (not a boolean)
  452. param = fromJS({
  453. required: true,
  454. type: "boolean",
  455. value: "test string"
  456. })
  457. result = validateParam( param, false )
  458. expect( result ).toEqual( ["Required field is not provided"] )
  459. // valid boolean value
  460. param = fromJS({
  461. required: true,
  462. type: "boolean",
  463. value: "true"
  464. })
  465. result = validateParam( param, false )
  466. expect( result ).toEqual( [] )
  467. // valid boolean value
  468. param = fromJS({
  469. required: true,
  470. type: "boolean",
  471. value: false
  472. })
  473. result = validateParam( param, false )
  474. expect( result ).toEqual( [] )
  475. })
  476. it("validates optional booleans", function() {
  477. // valid (empty) boolean value
  478. param = fromJS({
  479. required: false,
  480. type: "boolean",
  481. value: undefined
  482. })
  483. result = validateParam( param, false )
  484. expect( result ).toEqual( [] )
  485. // invalid boolean value (not a boolean)
  486. param = fromJS({
  487. required: false,
  488. type: "boolean",
  489. value: "test string"
  490. })
  491. result = validateParam( param, false )
  492. expect( result ).toEqual( ["Value must be a boolean"] )
  493. // valid boolean value
  494. param = fromJS({
  495. required: false,
  496. type: "boolean",
  497. value: "true"
  498. })
  499. result = validateParam( param, false )
  500. expect( result ).toEqual( [] )
  501. // valid boolean value
  502. param = fromJS({
  503. required: false,
  504. type: "boolean",
  505. value: false
  506. })
  507. result = validateParam( param, false )
  508. expect( result ).toEqual( [] )
  509. })
  510. it("validates required numbers", function() {
  511. // invalid number, string instead of a number
  512. param = fromJS({
  513. required: true,
  514. type: "number",
  515. value: "test"
  516. })
  517. result = validateParam( param, false )
  518. expect( result ).toEqual( ["Required field is not provided"] )
  519. // invalid number, undefined value
  520. param = fromJS({
  521. required: true,
  522. type: "number",
  523. value: undefined
  524. })
  525. result = validateParam( param, false )
  526. expect( result ).toEqual( ["Required field is not provided"] )
  527. // valid number with min and max
  528. param = fromJS({
  529. required: true,
  530. type: "number",
  531. value: 10,
  532. minimum: 5,
  533. maximum: 99
  534. })
  535. result = validateParam( param, false )
  536. expect( result ).toEqual( [] )
  537. // valid negative number with min and max
  538. param = fromJS({
  539. required: true,
  540. type: "number",
  541. value: -10,
  542. minimum: -50,
  543. maximum: -5
  544. })
  545. result = validateParam( param, false )
  546. expect( result ).toEqual( [] )
  547. // invalid number with maximum:0
  548. param = fromJS({
  549. required: true,
  550. type: "number",
  551. value: 1,
  552. maximum: 0
  553. })
  554. result = validateParam( param, false )
  555. expect( result ).toEqual( ["Value must be less than Maximum"] )
  556. // invalid number with minimum:0
  557. param = fromJS({
  558. required: true,
  559. type: "number",
  560. value: -10,
  561. minimum: 0
  562. })
  563. result = validateParam( param, false )
  564. expect( result ).toEqual( ["Value must be greater than Minimum"] )
  565. })
  566. it("validates optional numbers", function() {
  567. // invalid number, string instead of a number
  568. param = fromJS({
  569. required: false,
  570. type: "number",
  571. value: "test"
  572. })
  573. result = validateParam( param, false )
  574. expect( result ).toEqual( ["Value must be a number"] )
  575. // valid (empty) number
  576. param = fromJS({
  577. required: false,
  578. type: "number",
  579. value: undefined
  580. })
  581. result = validateParam( param, false )
  582. expect( result ).toEqual( [] )
  583. // valid number
  584. param = fromJS({
  585. required: false,
  586. type: "number",
  587. value: 10
  588. })
  589. result = validateParam( param, false )
  590. expect( result ).toEqual( [] )
  591. })
  592. it("validates required integers", function() {
  593. // invalid integer, string instead of an integer
  594. param = fromJS({
  595. required: true,
  596. type: "integer",
  597. value: "test"
  598. })
  599. result = validateParam( param, false )
  600. expect( result ).toEqual( ["Required field is not provided"] )
  601. // invalid integer, undefined value
  602. param = fromJS({
  603. required: true,
  604. type: "integer",
  605. value: undefined
  606. })
  607. result = validateParam( param, false )
  608. expect( result ).toEqual( ["Required field is not provided"] )
  609. // valid integer
  610. param = fromJS({
  611. required: true,
  612. type: "integer",
  613. value: 10
  614. })
  615. result = validateParam( param, false )
  616. expect( result ).toEqual( [] )
  617. })
  618. it("validates optional integers", function() {
  619. // invalid integer, string instead of an integer
  620. param = fromJS({
  621. required: false,
  622. type: "integer",
  623. value: "test"
  624. })
  625. result = validateParam( param, false )
  626. expect( result ).toEqual( ["Value must be an integer"] )
  627. // valid (empty) integer
  628. param = fromJS({
  629. required: false,
  630. type: "integer",
  631. value: undefined
  632. })
  633. result = validateParam( param, false )
  634. expect( result ).toEqual( [] )
  635. // integers
  636. param = fromJS({
  637. required: false,
  638. type: "integer",
  639. value: 10
  640. })
  641. result = validateParam( param, false )
  642. expect( result ).toEqual( [] )
  643. })
  644. })
  645. describe("fromJSOrdered", () => {
  646. it("should create an OrderedMap from an object", () => {
  647. const param = {
  648. value: "test"
  649. }
  650. const result = fromJSOrdered(param).toJS()
  651. expect( result ).toEqual( { value: "test" } )
  652. })
  653. it("should not use an object's length property for Map size", () => {
  654. const param = {
  655. length: 5
  656. }
  657. const result = fromJSOrdered(param).toJS()
  658. expect( result ).toEqual( { length: 5 } )
  659. })
  660. it("should create an OrderedMap from an array", () => {
  661. const param = [1, 1, 2, 3, 5, 8]
  662. const result = fromJSOrdered(param).toJS()
  663. expect( result ).toEqual( [1, 1, 2, 3, 5, 8] )
  664. })
  665. })
  666. describe("getAcceptControllingResponse", () => {
  667. it("should return the first 2xx response with a media type", () => {
  668. const responses = fromJSOrdered({
  669. "200": {
  670. content: {
  671. "application/json": {
  672. schema: {
  673. type: "object"
  674. }
  675. }
  676. }
  677. },
  678. "201": {
  679. content: {
  680. "application/json": {
  681. schema: {
  682. type: "object"
  683. }
  684. }
  685. }
  686. }
  687. })
  688. expect(getAcceptControllingResponse(responses)).toEqual(responses.get("200"))
  689. })
  690. it("should skip 2xx responses without defined media types", () => {
  691. const responses = fromJSOrdered({
  692. "200": {
  693. content: {
  694. "application/json": {
  695. schema: {
  696. type: "object"
  697. }
  698. }
  699. }
  700. },
  701. "201": {
  702. content: {
  703. "application/json": {
  704. schema: {
  705. type: "object"
  706. }
  707. }
  708. }
  709. }
  710. })
  711. expect(getAcceptControllingResponse(responses)).toEqual(responses.get("201"))
  712. })
  713. it("should default to the `default` response if it has defined media types", () => {
  714. const responses = fromJSOrdered({
  715. "200": {
  716. description: "quite empty"
  717. },
  718. "201": {
  719. description: "quite empty"
  720. },
  721. default: {
  722. content: {
  723. "application/json": {
  724. schema: {
  725. type: "object"
  726. }
  727. }
  728. }
  729. }
  730. })
  731. expect(getAcceptControllingResponse(responses)).toEqual(responses.get("default"))
  732. })
  733. it("should return null if there are no suitable controlling responses", () => {
  734. const responses = fromJSOrdered({
  735. "200": {
  736. description: "quite empty"
  737. },
  738. "201": {
  739. description: "quite empty"
  740. },
  741. "default": {
  742. description: "also empty.."
  743. }
  744. })
  745. expect(getAcceptControllingResponse(responses)).toBe(null)
  746. })
  747. it("should return null if an empty OrderedMap is passed", () => {
  748. const responses = fromJSOrdered()
  749. expect(getAcceptControllingResponse(responses)).toBe(null)
  750. })
  751. it("should return null if anything except an OrderedMap is passed", () => {
  752. const responses = {}
  753. expect(getAcceptControllingResponse(responses)).toBe(null)
  754. })
  755. })
  756. describe("createDeepLinkPath", function() {
  757. it("creates a deep link path replacing spaces with underscores", function() {
  758. const result = createDeepLinkPath("tag id with spaces")
  759. expect(result).toEqual("tag_id_with_spaces")
  760. })
  761. it("trims input when creating a deep link path", function() {
  762. let result = createDeepLinkPath(" spaces before and after ")
  763. expect(result).toEqual("spaces_before_and_after")
  764. result = createDeepLinkPath(" ")
  765. expect(result).toEqual("")
  766. })
  767. it("creates a deep link path with special characters", function() {
  768. const result = createDeepLinkPath("!@#$%^&*(){}[]")
  769. expect(result).toEqual("!@#$%^&*(){}[]")
  770. })
  771. it("returns an empty string for invalid input", function() {
  772. expect( createDeepLinkPath(null) ).toEqual("")
  773. expect( createDeepLinkPath(undefined) ).toEqual("")
  774. expect( createDeepLinkPath(1) ).toEqual("")
  775. expect( createDeepLinkPath([]) ).toEqual("")
  776. expect( createDeepLinkPath({}) ).toEqual("")
  777. })
  778. })
  779. describe("escapeDeepLinkPath", function() {
  780. it("creates and escapes a deep link path", function() {
  781. const result = escapeDeepLinkPath("tag id with spaces?")
  782. expect(result).toEqual("tag_id_with_spaces\\?")
  783. })
  784. it("escapes a deep link path that starts with a number", function() {
  785. const result = escapeDeepLinkPath("123")
  786. expect(result).toEqual("\\31 23")
  787. })
  788. it("escapes a deep link path with a class selector", function() {
  789. const result = escapeDeepLinkPath("hello.world")
  790. expect(result).toEqual("hello\\.world")
  791. })
  792. it("escapes a deep link path with an id selector", function() {
  793. const result = escapeDeepLinkPath("hello#world")
  794. expect(result).toEqual("hello\\#world")
  795. })
  796. })
  797. })