Skip to content

Commit ef5904f

Browse files
authored
Merge pull request #3668 from owenconti/test/3667-escaping-utils-tests
Deep link path util tests
2 parents 11d8a34 + 33ee880 commit ef5904f

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/core/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -652,5 +652,5 @@ export const shallowEqualKeys = (a,b, keys) => {
652652
})
653653
}
654654

655-
export const createDeepLinkPath = (str) => str ? str.replace(/\s/g, "_") : ""
655+
export const createDeepLinkPath = (str) => typeof str == "string" || str instanceof String ? str.trim().replace(/\s/g, "_") : ""
656656
export const escapeDeepLinkPath = (str) => cssEscape( createDeepLinkPath(str) )

test/core/utils.js

+51-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-env mocha */
22
import expect from "expect"
33
import { fromJS } from "immutable"
4-
import { mapToList, validateNumber, validateInteger, validateParam, validateFile, fromJSOrdered } from "core/utils"
4+
import { mapToList, validateNumber, validateInteger, validateParam, validateFile, fromJSOrdered, createDeepLinkPath, escapeDeepLinkPath } from "core/utils"
55
import win from "core/window"
66

77
describe("utils", function() {
@@ -581,5 +581,55 @@ describe("utils", function() {
581581
const result = fromJSOrdered(param).toJS()
582582
expect( result ).toEqual( [1, 1, 2, 3, 5, 8] )
583583
})
584+
})
585+
586+
describe("createDeepLinkPath", function() {
587+
it("creates a deep link path replacing spaces with underscores", function() {
588+
const result = createDeepLinkPath("tag id with spaces")
589+
expect(result).toEqual("tag_id_with_spaces")
590+
})
591+
592+
it("trims input when creating a deep link path", function() {
593+
let result = createDeepLinkPath(" spaces before and after ")
594+
expect(result).toEqual("spaces_before_and_after")
595+
596+
result = createDeepLinkPath(" ")
597+
expect(result).toEqual("")
584598
})
599+
600+
it("creates a deep link path with special characters", function() {
601+
const result = createDeepLinkPath("!@#$%^&*(){}[]")
602+
expect(result).toEqual("!@#$%^&*(){}[]")
603+
})
604+
605+
it("returns an empty string for invalid input", function() {
606+
expect( createDeepLinkPath(null) ).toEqual("")
607+
expect( createDeepLinkPath(undefined) ).toEqual("")
608+
expect( createDeepLinkPath(1) ).toEqual("")
609+
expect( createDeepLinkPath([]) ).toEqual("")
610+
expect( createDeepLinkPath({}) ).toEqual("")
611+
})
612+
})
613+
614+
describe("escapeDeepLinkPath", function() {
615+
it("creates and escapes a deep link path", function() {
616+
const result = escapeDeepLinkPath("tag id with spaces?")
617+
expect(result).toEqual("tag_id_with_spaces\\?")
618+
})
619+
620+
it("escapes a deep link path that starts with a number", function() {
621+
const result = escapeDeepLinkPath("123")
622+
expect(result).toEqual("\\31 23")
623+
})
624+
625+
it("escapes a deep link path with a class selector", function() {
626+
const result = escapeDeepLinkPath("hello.world")
627+
expect(result).toEqual("hello\\.world")
628+
})
629+
630+
it("escapes a deep link path with an id selector", function() {
631+
const result = escapeDeepLinkPath("hello#world")
632+
expect(result).toEqual("hello\\#world")
633+
})
634+
})
585635
})

0 commit comments

Comments
 (0)