@@ -2,16 +2,12 @@ import { render, RenderResult } from "@testing-library/react";
22import { Breadcrumbs , getCrumbs } from "./Breadcrumbs" ;
33import "@testing-library/jest-dom" ;
44
5+ const defaultArrayObject = [
6+ { name : "first" , href : "first/is/this" } ,
7+ { name : "second" , href : "second" } ,
8+ { name : "last one" , href : "last one" } ,
9+ ] ;
510describe ( "Breadcrumbs" , ( ) => {
6- const crumbFirst = "first" ,
7- crumbFirstTitle = "First" ,
8- crumbSecond = "second" ,
9- crumbSecondTitle = "Second" ,
10- crumbLast = "last one" ,
11- crumbLastTitle = "Last one" ,
12- defaultStringPath = `/${ crumbFirst } /${ crumbSecond } /${ crumbLast } ` ,
13- defaultArrayPath = [ crumbFirst , crumbSecond , crumbLast ] ;
14-
1511 function testHomeExists ( renderResult : RenderResult ) {
1612 const { getByTestId } = renderResult ;
1713 const homeIcon = getByTestId ( "HomeIcon" ) ;
@@ -20,48 +16,12 @@ describe("Breadcrumbs", () => {
2016 expect ( homeIcon . parentElement ) . toHaveAttribute ( "href" , "/" ) ;
2117 }
2218
23- function testCrumbsExist ( renderResult : RenderResult ) {
24- const { getAllByRole, getByRole, getByText, queryByRole } = renderResult ;
25-
26- expect ( getAllByRole ( "link" ) ) . toHaveLength ( 3 ) ;
27-
28- testHomeExists ( renderResult ) ;
29-
30- let crumb = getByRole ( "link" , { name : crumbFirstTitle } ) ;
31- expect ( crumb ) . toBeInTheDocument ( ) ;
32- expect ( crumb ) . toHaveAttribute ( "href" , `/${ crumbFirst } ` ) ;
33-
34- crumb = getByRole ( "link" , { name : crumbSecondTitle } ) ;
35- expect ( crumb ) . toBeInTheDocument ( ) ;
36- expect ( crumb ) . toHaveAttribute ( "href" , `/${ crumbFirst } /${ crumbSecond } ` ) ;
37-
38- expect (
39- queryByRole ( "link" , { name : crumbLastTitle } ) ,
40- ) . not . toBeInTheDocument ( ) ;
41- expect ( getByText ( crumbLastTitle ) ) . toBeInTheDocument ( ) ;
42- }
43-
4419 it ( "should render without errors" , ( ) => {
45- render ( < Breadcrumbs path = { defaultStringPath } /> ) ;
46- } ) ;
47-
48- it ( "should use a path as string" , ( ) => {
49- testCrumbsExist ( render ( < Breadcrumbs path = { defaultStringPath } /> ) ) ;
50- } ) ;
51-
52- it ( "should use a path as array" , ( ) => {
53- testCrumbsExist ( render ( < Breadcrumbs path = { defaultArrayPath } /> ) ) ;
20+ render ( < Breadcrumbs path = { defaultArrayObject } /> ) ;
5421 } ) ;
5522
5623 it ( "should show just home when an empty string" , ( ) => {
57- const renderResult = render ( < Breadcrumbs path = { "" } /> ) ;
58- testHomeExists ( renderResult ) ;
59- expect ( renderResult . getAllByRole ( "link" ) ) . toHaveLength ( 1 ) ;
60- } ) ;
61-
62- it ( "should show just home when an empty array" , ( ) => {
6324 const renderResult = render ( < Breadcrumbs path = { [ ] } /> ) ;
64-
6525 testHomeExists ( renderResult ) ;
6626 expect ( renderResult . getAllByRole ( "link" ) ) . toHaveLength ( 1 ) ;
6727 } ) ;
@@ -83,56 +43,23 @@ describe("getCrumbs", () => {
8343 } ,
8444 ] ;
8545
86- it ( "should match if path string" , ( ) => {
87- expect ( getCrumbs ( "/first/second/last one" ) ) . toStrictEqual ( correctCrumbs ) ;
88- } ) ;
89-
90- it ( "should match if last slash included" , ( ) => {
91- expect ( getCrumbs ( "/first/second/last one/" ) ) . toStrictEqual ( correctCrumbs ) ;
92- } ) ;
93-
94- it ( "should match if first slash excluded" , ( ) => {
95- expect ( getCrumbs ( "first/second/last one" ) ) . toStrictEqual ( correctCrumbs ) ;
96- } ) ;
97-
98- it ( "should match if first slash excluded and last slash included" , ( ) => {
99- expect ( getCrumbs ( "first/second/last one" ) ) . toStrictEqual ( correctCrumbs ) ;
100- } ) ;
101-
102- it ( "should match path string with multi separators" , ( ) => {
103- expect ( getCrumbs ( "///first//second/last one" ) ) . toStrictEqual ( correctCrumbs ) ;
104- } ) ;
105-
106- it ( "should return an empty array when an empty string is passed" , ( ) => {
107- expect ( getCrumbs ( "" ) ) . toStrictEqual ( [ ] ) ;
108- } ) ;
109-
110- it ( "should return an empty array when spaces are passed" , ( ) => {
111- expect ( getCrumbs ( " " ) ) . toStrictEqual ( [ ] ) ;
112- } ) ;
113-
114- it ( "should match if path array" , ( ) => {
115- expect ( getCrumbs ( [ "first" , "second" , "last one" ] ) ) . toStrictEqual (
116- correctCrumbs ,
117- ) ;
118- } ) ;
119-
120- it ( "should match if path array with empty" , ( ) => {
121- expect ( getCrumbs ( [ "first" , "second" , "last one" , "" ] ) ) . toStrictEqual (
122- correctCrumbs ,
123- ) ;
124- } ) ;
125-
126- it ( "should match by removing empty item" , ( ) => {
127- expect ( getCrumbs ( [ "first" , "second" , "last one" , "" ] ) ) . toStrictEqual (
128- correctCrumbs ,
129- ) ;
130- } ) ;
131-
132- it ( "should match by removing spaces only" , ( ) => {
133- expect ( getCrumbs ( [ "first" , "second" , "last one" , " " ] ) ) . toStrictEqual (
134- correctCrumbs ,
135- ) ;
46+ it ( "should match with correct array object passed" , ( ) => {
47+ expect (
48+ getCrumbs ( [
49+ {
50+ name : "first" ,
51+ href : "/first" ,
52+ } ,
53+ {
54+ name : "second" ,
55+ href : "/first/second" ,
56+ } ,
57+ {
58+ name : "last one" ,
59+ href : "/first/second/last one" ,
60+ } ,
61+ ] )
62+ ) . toStrictEqual ( correctCrumbs ) ;
13663 } ) ;
13764
13865 it ( "should return an empty array when an empty array is passed" , ( ) => {
0 commit comments