11import { getDefaultConfig } from "../../../configManager/config.default" ;
2- import { PublicLogger } from "../../../logger/logger" ;
32import { IConfig } from "../../../types/types" ;
43import { generateStartEditingButton } from "./../../generators/generateStartEditingButton" ;
54
65describe ( "generateStartEditingButton" , ( ) => {
76 let config : IConfig ;
8- let visualBuilderContainer : HTMLDivElement ;
97
108 beforeEach ( ( ) => {
119 config = getDefaultConfig ( ) ;
1210
1311 config . stackDetails . apiKey = "bltapikey" ;
1412 config . stackDetails . environment = "bltenvironment" ;
15-
16- visualBuilderContainer = document . createElement ( "div" ) ;
17- document . body . appendChild ( visualBuilderContainer ) ;
1813 } ) ;
1914
2015 afterEach ( ( ) => {
2116 vi . clearAllMocks ( ) ;
22- document . body . removeChild ( visualBuilderContainer ) ;
17+ const existingButton = document . querySelector (
18+ ".visual-builder__start-editing-btn"
19+ ) ;
20+ if ( existingButton ) {
21+ existingButton . remove ( ) ;
22+ }
2323 } ) ;
2424
25- test ( "should return an anchor tag" , ( ) => {
26- const button = generateStartEditingButton ( visualBuilderContainer ) ;
25+ test ( "should return an anchor tag" , ( ) => {
26+ const button = generateStartEditingButton ( ) ;
2727 expect ( button ) . toBeInstanceOf ( HTMLAnchorElement ) ;
2828 } ) ;
2929
30- test ( "should append the button within visualBuilderContainer " , ( ) => {
31- expect ( visualBuilderContainer . children . length ) . toBe ( 0 ) ;
32- generateStartEditingButton ( visualBuilderContainer ) ;
30+ test ( "should append the button within document.body " , ( ) => {
31+ const initialBodyChildren = document . body . children . length ;
32+ generateStartEditingButton ( ) ;
3333
34- expect ( visualBuilderContainer . children . length ) . toBe ( 1 ) ;
34+ expect ( document . body . children . length ) . toBe ( initialBodyChildren + 1 ) ;
3535 } ) ;
3636
3737 test ( "should update the href when clicked" , ( ) => {
38- const button = generateStartEditingButton ( visualBuilderContainer ) ;
38+ const button = generateStartEditingButton ( ) ;
3939 button ?. click ( ) ;
4040
4141 expect ( button ?. getAttribute ( "href" ) ) . toBe (
4242 "https://app.contentstack.com/#!/stack//visual-builder?branch=main&target-url=http%3A%2F%2Flocalhost%3A3000%2F&locale=en-us"
4343 ) ;
4444 } ) ;
4545
46- test ( "should get the href detal from cslp attribute if present" , ( ) => {
46+ test ( "should get the href detail from cslp attribute if present" , ( ) => {
4747 const h1 = document . createElement ( "h1" ) ;
4848
4949 h1 . setAttribute (
@@ -53,19 +53,11 @@ describe("generateStartEditingButton", () => {
5353
5454 document . body . appendChild ( h1 ) ;
5555
56- const button = generateStartEditingButton ( visualBuilderContainer ) ;
56+ const button = generateStartEditingButton ( ) ;
5757 button ?. click ( ) ;
5858
5959 expect ( button ?. getAttribute ( "href" ) ) . toBe (
6060 "https://app.contentstack.com/#!/stack//visual-builder?branch=main&target-url=http%3A%2F%2Flocalhost%3A3000%2F&locale=en-us"
6161 ) ;
6262 } ) ;
63-
64- test ( "should throw a warning if visualBuilderContainer is not found" , ( ) => {
65- const spiedWarn = vi . spyOn ( PublicLogger , "warn" ) ;
66-
67- generateStartEditingButton ( null ) ;
68-
69- expect ( spiedWarn ) . toHaveBeenCalledTimes ( 1 ) ;
70- } ) ;
7163} ) ;
0 commit comments