Skip to content

Commit

Permalink
Added support for opening a new tab with the redirect with identity a…
Browse files Browse the repository at this point in the history
…ction (#463)
  • Loading branch information
jonsnyder authored Feb 20, 2025
1 parent b002f8a commit 78b3405
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/
module.exports =
({ instanceManager, document, logger, getConfigOverrides }) =>
({ instanceManager, window, logger, getConfigOverrides }) =>
(settings, event) => {
const { instanceName } = settings;
const instance = instanceManager.getInstance(instanceName);
Expand Down Expand Up @@ -42,11 +42,12 @@ module.exports =

const url = event.element.href;
const edgeConfigOverrides = getConfigOverrides(settings);
const target = event.element.target || "_self";

return instance("appendIdentityToUrl", {
url,
edgeConfigOverrides,
}).then(({ url: newLocation }) => {
document.location = newLocation;
window.open(newLocation, target);
});
};
4 changes: 1 addition & 3 deletions src/lib/actions/redirectWithIdentity/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ const createRedirectWithIdentity = require("./createRedirectWithIdentity");
const instanceManager = require("../../instanceManager/index");
const createGetConfigOverrides = require("../../utils/createGetConfigOverrides");

const document = window.document;

module.exports = createRedirectWithIdentity({
instanceManager,
document,
window,
logger: turbine.logger,
getConfigOverrides: createGetConfigOverrides(turbine.environment?.stage),
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import createRedirectWithIdentity from "../../../../../src/lib/actions/redirectW
describe("createRedirectWithIdentity", () => {
let instanceManager;
let instance;
let document;
let window;
let redirectWithIdentity;
let event;
let logger;
Expand All @@ -29,7 +29,7 @@ describe("createRedirectWithIdentity", () => {
instance = vi.fn();
instanceManager.getInstance.mockReturnValue(instance);
instance.mockResolvedValue({ url: "newurl" });
document = { location: "originalLocation" };
window = { open: vi.fn() };
getConfigOverrides = vi.fn();
event = {
nativeEvent: {
Expand All @@ -45,7 +45,7 @@ describe("createRedirectWithIdentity", () => {

redirectWithIdentity = createRedirectWithIdentity({
instanceManager,
document,
window,
logger,
getConfigOverrides,
});
Expand All @@ -59,14 +59,14 @@ describe("createRedirectWithIdentity", () => {
);
expect(returnValue).toBeUndefined();
expect(instanceManager.getInstance).toHaveBeenCalledWith("myinstance");
expect(document.location).toEqual("originalLocation");
expect(window.open).not.toHaveBeenCalled();
expect(event.nativeEvent.preventDefault).not.toHaveBeenCalled();
expect(logger.warn).toHaveBeenCalled();
});

it("doesn't redirect when there is no nativeEvent", async () => {
await redirectWithIdentity({ instanceName: "myinstance" }, {});
expect(document.location).toEqual("originalLocation");
expect(window.open).not.toHaveBeenCalled();
expect(logger.warn).toHaveBeenCalled();
});

Expand All @@ -77,7 +77,7 @@ describe("createRedirectWithIdentity", () => {
nativeEvent: {},
},
);
expect(document.location).toEqual("originalLocation");
expect(window.open).not.toHaveBeenCalled();
expect(logger.warn).toHaveBeenCalled();
});

Expand All @@ -91,7 +91,7 @@ describe("createRedirectWithIdentity", () => {
},
},
);
expect(document.location).toBe("newurl");
expect(window.open).toHaveBeenCalledWith("newurl", "_self");
expect(logger.warn).not.toHaveBeenCalled();
expect(instance).toHaveBeenCalledWith("appendIdentityToUrl", {
url: "originalHref",
Expand All @@ -102,7 +102,13 @@ describe("createRedirectWithIdentity", () => {
it("redirects", async () => {
await redirectWithIdentity({ instanceName: "myinstance" }, event);
expect(event.nativeEvent.preventDefault).toHaveBeenCalledTimes(1);
expect(document.location).toBe("newurl");
expect(window.open).toHaveBeenCalledWith("newurl", "_self");
});

it("redirects with target", async () => {
event.element.target = "_blank";
await redirectWithIdentity({ instanceName: "myinstance" }, event);
expect(window.open).toHaveBeenCalledWith("newurl", "_blank");
});

it("redirects with edge config overrides", async () => {
Expand Down

0 comments on commit 78b3405

Please sign in to comment.