镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content
This repository was archived by the owner on Dec 7, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/components/FeatureFlagRenderer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* @flow */
import React, { Component } from "react";

import { ldBrowserInit } from "../lib/launchDarkly";
import { ldBrowserInit, ldOverrideFlag } from "../lib/launchDarkly";
import { FeatureFlagType } from "../types/FeatureFlag";

type LaunchDarklyConfig = {
Expand Down Expand Up @@ -61,8 +61,11 @@ export default class FeatureFlagRenderer extends Component {
ldClient.on("ready", () => {
const showFeature = ldClient.variation(flagKey, false);
const defaultState = { checkFeatureFlagComplete: true };
const override = ldOverrideFlag(flagKey);

if (showFeature) {
if (typeof override !== "undefined"){
this.setState({ showFeature: override, ...defaultState});
} else if (showFeature) {
this.setState({ showFeature: true, ...defaultState });
} else {
this.setState({ showFeature: false, ...defaultState });
Expand Down
39 changes: 39 additions & 0 deletions src/lib/launchDarkly.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,48 @@
import launchDarklyBrowser from "ldclient-js";
const url = require("url");

export function getLocation() {
if (window.location) {
return window.location.toString();
}
return "";
}

export function ldBrowserInit (key, user) {
return launchDarklyBrowser.initialize(key, user);
}

export function ldOverrideFlag(flagKey) {
let override;
/*
POST /users?features=send-onboarding-email
# Overrides the `send-onboarding-email` boolean feature flag, setting it to `true`
GET /users/101?features=show-user-email,user-nicknames,hide-inactive-users
# Enables the `show-user-email`, `user-nicknames`, and `hide-inactive-users` feature flags
POST /users?features.verify-email=false&features.email-frequency=weekly
# Disables the `verify-email` feature flag and sets the `email-frequency` variation to "weekly"
*/
const query = url.parse(exports.getLocation(), true).query;
const queryFlag = query["features." + flagKey];

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works for supporting the advanced overriding, e.g., ?features.flagA=true but I'm not seeing support for the basic list of boolean flags, e.g., ?features=flagA,flagB,flagC.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right and I will fix that.

const queryFeatures = query["features"];

if (typeof queryFlag !== "undefined"){
if (queryFlag === ""){
override = true;
} else if (queryFlag === "false"){
override = false;
}
} else if (queryFeatures) {
queryFeatures.split(",").forEach((f) => {
if (f === flagKey){
override = true;
}
});
}

return override;
}

export function getAllFeatureFlags (key, user) {
const ldClient = ldBrowserInit(key, user);
return new Promise((resolve) => {
Expand Down
6 changes: 6 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"env": {
"mocha": true
}
}

71 changes: 52 additions & 19 deletions test/components/FeatureFlagRenderer.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import { shallow, mount } from "enzyme";
import { expect } from "chai";
import { spy, stub } from "sinon";
import { spy, stub, sandbox } from "sinon";

import FeatureFlagRenderer from "../../src/components/FeatureFlagRenderer";
import * as launchDarkly from "../../src/lib/launchDarkly";
Expand Down Expand Up @@ -148,11 +148,18 @@ describe("components/FeatureFlagRenderer", () => {
});

describe("the _checkFeatureFlag function", () => {
before(() => {
const variation = stub();
variation.onCall(0).returns(true);
variation.onCall(1).returns(false);

const variation = stub();
const getWrapper = () => {
return mount(
<FeatureFlagRenderer
launchDarklyConfig={launchDarklyConfig}
flagKey="my-test"
renderFeatureCallback={renderFeatureCallback}
/>
);
};
before(() => {
const ldClientStub = stub().returns({
on: (ready, callback) => {
callback();
Expand All @@ -164,28 +171,54 @@ describe("components/FeatureFlagRenderer", () => {

context("when the feature flag comes back true", () => {
it("sets the state", () => {
const wrapper = mount(
<FeatureFlagRenderer
launchDarklyConfig={launchDarklyConfig}
flagKey="my-test"
renderFeatureCallback={renderFeatureCallback}
/>
);
variation.returns(true);
const wrapper = getWrapper();
expect(wrapper.state()).to.deep.equal({ checkFeatureFlagComplete: true, showFeature: true });
});
});

context("when the feature flag comes back false", () => {
it("sets the state", () => {
const wrapper = mount(
<FeatureFlagRenderer
launchDarklyConfig={launchDarklyConfig}
flagKey="my-test"
renderFeatureCallback={renderFeatureCallback}
/>
);
variation.returns(false);
const wrapper = getWrapper();
expect(wrapper.state()).to.deep.equal({ checkFeatureFlagComplete: true, showFeature: false });
});
});

describe("query param flag overrides if not undefined", () => {
let _sandbox;
before(() => {
_sandbox = sandbox.create();
});
afterEach(() => {
_sandbox.restore();
});
it("param 'features.flag=false' overrides LD data 'on'", () => {
variation.returns(true);
sandbox.stub(launchDarkly, "getLocation").returns("http://ab.cdef.com?features.my-test=false");
const wrapper = getWrapper();
expect(wrapper.state()).to.deep.equal({ checkFeatureFlagComplete: true, showFeature: false });
});
it("param 'features.flag' overrides LD data 'off'", () => {
variation.returns(false);
sandbox.stub(launchDarkly, "getLocation").returns("http://ab.cdef.com?features.my-test");
const wrapper = getWrapper();
expect(wrapper.state()).to.deep.equal({ checkFeatureFlagComplete: true, showFeature: true });
});
it("param 'features=flag' overrides LD data 'off'", () => {
variation.returns(false);
sandbox.stub(launchDarkly, "getLocation").returns("httpd://ab.cdef.com?features=my-test");
const wrapper = getWrapper();
expect(wrapper.state()).to.deep.equal({ checkFeatureFlagComplete: true, showFeature: true });
});
it("param comma-list of features to enable", () => {
variation.withArgs("one").returns(false);
variation.withArgs("my-test").returns(false);
variation.withArgs("two").returns(false);
sandbox.stub(launchDarkly, "getLocation").returns("http://ab.cdef.com?features=one,my-test");
const wrapper = getWrapper();
expect(wrapper.state()).to.deep.equal({ checkFeatureFlagComplete: true, showFeature: true });
});
});
});
});
Expand Down
32 changes: 32 additions & 0 deletions test/lib/launchDarkly.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { expect } from "chai";
import { spy } from "sinon";
import * as ldClient from "./../../src/lib/launchDarkly.js";
import launchDarklyBrowser from "ldclient-js";

describe("lib/launchDarkly", () => {
it("exports three functions", () => {
expect(ldClient).to.have.property("ldBrowserInit");
expect(ldClient).to.have.property("ldOverrideFlag");
expect(ldClient).to.have.property("getAllFeatureFlags");
});

describe("ldBroswerInit", () => {
it("proxies to ldclient-js", () => {
const key = "my key";
const user = "my user";
spy(launchDarklyBrowser, "initialize").withArgs(key, user);
ldClient.ldBrowserInit(key, user);

// this is misuse of sinon; the expectation fails
//expect(launchDarklyBrowser.initialize.withArgs(key, user).calledOnce).to.be.ok()

});
});
describe("getAllFeatureFlags", () => {

});
describe("ldOverrideFlag", () => {

});

});