EmbeddedBackupRestore.test.jsx (3721B)
1 import React from "react"; 2 import { mount } from "enzyme"; 3 import { EmbeddedBackupRestore } from "content-src/components/EmbeddedBackupRestore"; 4 import { GlobalOverrider } from "asrouter/tests/unit/utils"; 5 6 describe("EmbeddedBackupRestore component", () => { 7 let wrapper; 8 let globals; 9 let sandbox; 10 11 beforeEach(() => { 12 sandbox = sinon.createSandbox(); 13 globals = new GlobalOverrider(); 14 globals.set({ AWSendToParent: sandbox.stub() }); 15 globals.set({ 16 AWSendToParent: sandbox.stub(), 17 AWFindBackupsInWellKnownLocations: sandbox.stub().resolves({ 18 found: false, 19 multipleBackupsFound: false, 20 backupFileToRestore: null, 21 }), 22 }); 23 }); 24 25 afterEach(() => { 26 sandbox.restore(); 27 globals.restore(); 28 if (wrapper) { 29 wrapper.unmount(); 30 } 31 }); 32 33 it("should render the restore-from-backup element", () => { 34 wrapper = mount(<EmbeddedBackupRestore />); 35 36 const restoreFromBackupElement = wrapper.find("restore-from-backup"); 37 assert.ok( 38 restoreFromBackupElement.exists(), 39 "restore-from-backup element should be rendered" 40 ); 41 42 assert.equal( 43 restoreFromBackupElement.prop("aboutWelcomeEmbedded"), 44 "true", 45 "aboutWelcomeEmbedded should be set to 'true'" 46 ); 47 48 assert.equal( 49 restoreFromBackupElement.prop("labelFontWeight"), 50 "600", 51 "labelFontWeight should be set to '600'" 52 ); 53 }); 54 55 it("calls AWFindBackupsInWellKnownLocations on mount", async () => { 56 wrapper = mount(<EmbeddedBackupRestore />); 57 58 // Ensure the effect runs before we assert. 59 await new Promise(resolve => setTimeout(resolve, 0)); 60 61 assert.isTrue( 62 window.AWFindBackupsInWellKnownLocations.calledOnce, 63 "should call AWFindBackupsInWellKnownLocations exactly once on mount" 64 ); 65 }); 66 67 it("renders skip button when provided and applies arrow icon class, and calls handleAction", async () => { 68 const handleAction = sandbox.stub(); 69 const skipButton = { 70 label: { raw: "Skip" }, 71 has_arrow_icon: true, 72 }; 73 74 wrapper = mount( 75 <EmbeddedBackupRestore 76 handleAction={handleAction} 77 skipButton={skipButton} 78 /> 79 ); 80 81 const button = wrapper.find("#secondary_button"); 82 assert.ok(button.exists(), "skip button rendered"); 83 assert.isTrue( 84 button.hasClass("secondary") && button.hasClass("arrow-icon"), 85 "arrow icon class is applied when has_arrow_icon is true" 86 ); 87 88 button.simulate("click"); 89 sinon.assert.calledOnce(handleAction); 90 }); 91 92 it("updates skip button state based on BackupUI:RecoveryProgress events", async () => { 93 const skipButton = { label: { string_id: "skip" } }; 94 wrapper = mount(<EmbeddedBackupRestore skipButton={skipButton} />); 95 const node = wrapper.find("restore-from-backup").getDOMNode(); 96 97 node.dispatchEvent( 98 new CustomEvent("BackupUI:RecoveryProgress", { 99 detail: { recoveryInProgress: true }, 100 }) 101 ); 102 103 wrapper.update(); 104 let button = wrapper.find("#secondary_button"); 105 assert.isTrue(button.prop("disabled")); 106 assert.equal(button.prop("aria-busy"), true); 107 108 node.dispatchEvent( 109 new CustomEvent("BackupUI:RecoveryProgress", { 110 detail: { recoveryInProgress: false }, 111 }) 112 ); 113 114 wrapper.update(); 115 button = wrapper.find("#secondary_button"); 116 assert.isFalse(button.prop("disabled")); 117 assert.isUndefined(button.prop("aria-busy")); 118 }); 119 120 it("does not render skip button section when skipButton prop is missing", () => { 121 wrapper = mount(<EmbeddedBackupRestore />); 122 assert.isFalse(wrapper.find("#secondary_button").exists()); 123 assert.isFalse(wrapper.find(".secondary-cta").exists()); 124 }); 125 });