1- import { Mock } from "vitest" ;
1+ import { Mock , vitest } from "vitest" ;
22import { DOMRect } from "../../../__test__/utils" ;
33import {
44 createMultipleEditButton ,
55 createSingularEditButton ,
66 getEditButtonPosition ,
7+ shouldRenderEditButton
78} from "../editButton" ;
9+ import Config from "../../../configManager/configManager" ;
10+ import * as inIframe from "../../../common/inIframe" ;
11+ import { PublicLogger } from "../../../logger/logger" ;
812
913let editCallback : Mock < ( e : MouseEvent ) => void > | undefined ;
1014let linkCallback : Mock < ( e : MouseEvent ) => void > | undefined ;
@@ -241,3 +245,75 @@ describe("Edit button for Link", () => {
241245 expect ( linkCallback ) . toBeCalled ( ) ;
242246 } ) ;
243247} ) ;
248+
249+ describe ( "shouldRenderEditButton" , ( ) => {
250+ test ( "should return true if the config has enabled as true" , ( ) => {
251+ vitest . spyOn ( Config , "get" ) . mockReturnValue ( { editButton : { enable : true } } ) ;
252+ expect ( shouldRenderEditButton ( ) ) . toBe ( true ) ;
253+ } ) ;
254+ test ( "should return false if the config has enabled as false" , ( ) => {
255+ vitest . spyOn ( Config , "get" ) . mockReturnValue ( { editButton : { enable : false } } ) ;
256+ expect ( shouldRenderEditButton ( ) ) . toBe ( false ) ;
257+ } ) ;
258+
259+ describe ( "includeByQueryParameter" , ( ) => {
260+ test ( "should log error and return false if enable key is undefined" , ( ) => {
261+ const loggerSpy = vitest . spyOn ( PublicLogger , "error" ) ;
262+ vitest . spyOn ( Config , "get" ) . mockReturnValue ( { editButton : { } } ) ;
263+ expect ( shouldRenderEditButton ( ) ) . toBe ( false ) ;
264+ expect ( loggerSpy ) . toHaveBeenCalledWith ( "enable key is required inside editButton object" ) ;
265+ } ) ;
266+
267+ test ( "should return true if cslp-buttons query parameter is true" , ( ) => {
268+ vitest . spyOn ( Config , "get" ) . mockReturnValue ( { editButton : { enable : true } } ) ;
269+ vitest . spyOn ( window , "location" , "get" ) . mockReturnValue ( {
270+ href : "http://example.com?cslp-buttons=true" ,
271+ } as Location ) ;
272+ expect ( shouldRenderEditButton ( ) ) . toBe ( true ) ;
273+ } ) ;
274+
275+ test ( "should return false if cslp-buttons query parameter is false" , ( ) => {
276+ vitest . spyOn ( Config , "get" ) . mockReturnValue ( { editButton : { enable : true } } ) ;
277+ vitest . spyOn ( window , "location" , "get" ) . mockReturnValue ( {
278+ href : "http://example.com?cslp-buttons=false" ,
279+ } as Location ) ;
280+ expect ( shouldRenderEditButton ( ) ) . toBe ( false ) ;
281+ } ) ;
282+
283+ test ( "should return true if cslp-buttons query parameter is not present" , ( ) => {
284+ vitest . spyOn ( Config , "get" ) . mockReturnValue ( { editButton : { enable : true } } ) ;
285+ vitest . spyOn ( window , "location" , "get" ) . mockReturnValue ( {
286+ href : "http://example.com" ,
287+ } as Location ) ;
288+ expect ( shouldRenderEditButton ( ) ) . toBe ( true ) ;
289+ } ) ;
290+ } )
291+ describe ( "exclude" , ( ) => {
292+ test ( "should return false if the config has exclude as `insideLivePreviewPortal` and the element is inside live preview portal" , ( ) => {
293+ vitest . spyOn ( inIframe , "inIframe" ) . mockReturnValue ( true ) ;
294+ vitest . spyOn ( Config , "get" ) . mockReturnValue ( { editButton : { enable : true , exclude : [ "insideLivePreviewPortal" ] } } ) ;
295+ expect ( shouldRenderEditButton ( ) ) . toBe ( false ) ;
296+ } ) ;
297+ test ( "should return true if the config has exclude as `insideLivePreviewPortal` and the element is not inside live preview portal" , ( ) => {
298+ vitest . spyOn ( inIframe , "inIframe" ) . mockReturnValue ( false ) ;
299+ vitest . spyOn ( Config , "get" ) . mockReturnValue ( { editButton : { enable : true , exclude : [ "insideLivePreviewPortal" ] } } ) ;
300+ expect ( shouldRenderEditButton ( ) ) . toBe ( true ) ;
301+ } ) ;
302+ test ( "should return false if the config has exclude as `outsideLivePreviewPortal` and the element is not inside live preview portal" , ( ) => {
303+ vitest . spyOn ( inIframe , "inIframe" ) . mockReturnValue ( false ) ;
304+ vitest . spyOn ( Config , "get" ) . mockReturnValue ( { editButton : { enable : true , exclude : [ "outsideLivePreviewPortal" ] } } ) ;
305+ expect ( shouldRenderEditButton ( ) ) . toBe ( false ) ;
306+ } ) ;
307+ test ( "should return true if the config has exclude as `outsideLivePreviewPortal` and the element is inside live preview portal" , ( ) => {
308+ vitest . spyOn ( inIframe , "inIframe" ) . mockReturnValue ( true ) ;
309+ vitest . spyOn ( Config , "get" ) . mockReturnValue ( { editButton : { enable : true , exclude : [ "outsideLivePreviewPortal" ] } } ) ;
310+ expect ( shouldRenderEditButton ( ) ) . toBe ( true ) ;
311+ } ) ;
312+ } )
313+
314+ test ( "should return false if the website is rendered in Builder" , ( ) => {
315+ vitest . spyOn ( Config , "get" ) . mockReturnValue ( { editButton : { enable : true } , windowType : "builder" } ) ;
316+ vitest . spyOn ( inIframe , "inIframe" ) . mockReturnValue ( true ) ;
317+ expect ( shouldRenderEditButton ( ) ) . toBe ( false ) ;
318+ } )
319+ } )
0 commit comments