-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🪟🔧 Refactor feature service #14559
Merged
Merged
🪟🔧 Refactor feature service #14559
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a9a951a
Refactor feature service
timroes d83aebe
Merge branch 'master' into tim/refactor-feature-service
timroes ed14d45
Merge branch 'master' into tim/refactor-feature-service
timroes d662e1a
Use new feature service code
timroes f1b0228
Link up feature service to LaunchDarkly
timroes 33b3a66
Remove debug code
timroes 87377bd
Merge branch 'master' into tim/refactor-feature-service
timroes 9580492
Merge branch 'master' into tim/refactor-feature-service
23a6503
Rename OnlyWithFeature to IfFeatureEnabled
c974745
Change useEffect dep
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
252 changes: 184 additions & 68 deletions
252
airbyte-webapp/src/hooks/services/Feature/FeatureService.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,204 @@ | ||
import { act, renderHook } from "@testing-library/react-hooks"; | ||
import React from "react"; | ||
import { render } from "@testing-library/react"; | ||
import { renderHook } from "@testing-library/react-hooks"; | ||
import { useEffect } from "react"; | ||
|
||
import { ConfigContext, ConfigContextData } from "config"; | ||
import { TestWrapper } from "utils/testutils"; | ||
|
||
import { FeatureService, useFeatureRegisterValues, useFeatureService } from "./FeatureService"; | ||
import { FeatureItem } from "./types"; | ||
|
||
const predefinedFeatures = [ | ||
{ | ||
id: FeatureItem.AllowCustomDBT, | ||
}, | ||
]; | ||
import { FeatureService, IfFeatureEnabled, useFeature, useFeatureService } from "./FeatureService"; | ||
import { FeatureItem, FeatureSet } from "./types"; | ||
|
||
const wrapper: React.FC = ({ children }) => ( | ||
<TestWrapper> | ||
<ConfigContext.Provider | ||
value={ | ||
{ | ||
config: { features: predefinedFeatures }, | ||
} as unknown as ConfigContextData | ||
} | ||
> | ||
<FeatureService>{children}</FeatureService> | ||
</ConfigContext.Provider> | ||
</TestWrapper> | ||
<FeatureService features={[FeatureItem.AllowCreateConnection, FeatureItem.AllowSync]}>{children}</FeatureService> | ||
); | ||
|
||
describe("FeatureService", () => { | ||
test("should register and unregister features", async () => { | ||
const { result } = renderHook(() => useFeatureService(), { | ||
wrapper, | ||
type FeatureOverwrite = FeatureItem[] | FeatureSet | undefined; | ||
|
||
interface FeatureOverwrites { | ||
workspace?: FeatureOverwrite; | ||
user?: FeatureOverwrite; | ||
overwrite?: FeatureOverwrite; | ||
} | ||
|
||
/** | ||
* Test utility method to wrap setting all the different level of features, rerender | ||
* with a different set of features and getting the merged feature set. | ||
*/ | ||
const getFeatures = (initialProps: FeatureOverwrites) => { | ||
return renderHook( | ||
({ overwrite, user, workspace }: FeatureOverwrites) => { | ||
const { features, setWorkspaceFeatures, setUserFeatures, setFeatureOverwrites } = useFeatureService(); | ||
useEffect(() => { | ||
setWorkspaceFeatures(workspace); | ||
}, [setWorkspaceFeatures, workspace]); | ||
useEffect(() => { | ||
setUserFeatures(user); | ||
}, [setUserFeatures, user]); | ||
useEffect(() => { | ||
setFeatureOverwrites(overwrite); | ||
}, [overwrite, setFeatureOverwrites]); | ||
return features; | ||
}, | ||
{ wrapper, initialProps } | ||
); | ||
}; | ||
|
||
describe("Feature Service", () => { | ||
describe("FeatureService", () => { | ||
it("should allow setting default features", () => { | ||
const getFeature = (feature: FeatureItem) => renderHook(() => useFeature(feature), { wrapper }).result.current; | ||
expect(getFeature(FeatureItem.AllowCreateConnection)).toBe(true); | ||
expect(getFeature(FeatureItem.AllowCustomDBT)).toBe(false); | ||
expect(getFeature(FeatureItem.AllowSync)).toBe(true); | ||
expect(getFeature(FeatureItem.AllowUpdateConnectors)).toBe(false); | ||
}); | ||
|
||
it("workspace features should merge correctly with default features", () => { | ||
expect( | ||
getFeatures({ | ||
workspace: [FeatureItem.AllowCustomDBT, FeatureItem.AllowUploadCustomImage], | ||
}).result.current.sort() | ||
).toEqual([ | ||
FeatureItem.AllowCreateConnection, | ||
FeatureItem.AllowCustomDBT, | ||
FeatureItem.AllowSync, | ||
FeatureItem.AllowUploadCustomImage, | ||
]); | ||
}); | ||
|
||
expect(result.current.features).toEqual(predefinedFeatures); | ||
it("workspace features can disable default features", () => { | ||
expect( | ||
getFeatures({ | ||
workspace: { [FeatureItem.AllowCustomDBT]: true, [FeatureItem.AllowCreateConnection]: false } as FeatureSet, | ||
}).result.current.sort() | ||
).toEqual([FeatureItem.AllowCustomDBT, FeatureItem.AllowSync]); | ||
}); | ||
|
||
act(() => { | ||
result.current.registerFeature([ | ||
{ | ||
id: FeatureItem.AllowCreateConnection, | ||
}, | ||
it("user features should merge correctly with workspace and default features", () => { | ||
expect( | ||
getFeatures({ | ||
workspace: [FeatureItem.AllowCustomDBT, FeatureItem.AllowUploadCustomImage], | ||
user: [FeatureItem.AllowOAuthConnector], | ||
}).result.current.sort() | ||
).toEqual([ | ||
FeatureItem.AllowCreateConnection, | ||
FeatureItem.AllowCustomDBT, | ||
FeatureItem.AllowOAuthConnector, | ||
FeatureItem.AllowSync, | ||
FeatureItem.AllowUploadCustomImage, | ||
]); | ||
}); | ||
|
||
expect(result.current.features).toEqual([ | ||
...predefinedFeatures, | ||
{ | ||
id: FeatureItem.AllowCreateConnection, | ||
}, | ||
]); | ||
it("user features can disable workspace and default features", () => { | ||
expect( | ||
getFeatures({ | ||
workspace: [FeatureItem.AllowCustomDBT, FeatureItem.AllowUploadCustomImage], | ||
user: { | ||
[FeatureItem.AllowOAuthConnector]: true, | ||
[FeatureItem.AllowUploadCustomImage]: false, | ||
[FeatureItem.AllowCreateConnection]: false, | ||
} as FeatureSet, | ||
}).result.current.sort() | ||
).toEqual([FeatureItem.AllowCustomDBT, FeatureItem.AllowOAuthConnector, FeatureItem.AllowSync]); | ||
}); | ||
|
||
act(() => { | ||
result.current.unregisterFeature([FeatureItem.AllowCreateConnection]); | ||
it("user features can re-enable feature that are disabled per workspace", () => { | ||
expect( | ||
getFeatures({ | ||
workspace: { [FeatureItem.AllowCustomDBT]: true, [FeatureItem.AllowSync]: false } as FeatureSet, | ||
user: [FeatureItem.AllowOAuthConnector, FeatureItem.AllowSync], | ||
}).result.current.sort() | ||
).toEqual([ | ||
FeatureItem.AllowCreateConnection, | ||
FeatureItem.AllowCustomDBT, | ||
FeatureItem.AllowOAuthConnector, | ||
FeatureItem.AllowSync, | ||
]); | ||
}); | ||
|
||
expect(result.current.features).toEqual(predefinedFeatures); | ||
it("overwritte features can overwrite workspace and user features", () => { | ||
expect( | ||
getFeatures({ | ||
workspace: { [FeatureItem.AllowCustomDBT]: true, [FeatureItem.AllowSync]: false } as FeatureSet, | ||
user: { | ||
[FeatureItem.AllowOAuthConnector]: true, | ||
[FeatureItem.AllowSync]: true, | ||
[FeatureItem.AllowCreateConnection]: false, | ||
} as FeatureSet, | ||
overwrite: [FeatureItem.AllowUploadCustomImage, FeatureItem.AllowCreateConnection], | ||
}).result.current.sort() | ||
).toEqual([ | ||
FeatureItem.AllowCreateConnection, | ||
FeatureItem.AllowCustomDBT, | ||
FeatureItem.AllowOAuthConnector, | ||
FeatureItem.AllowSync, | ||
FeatureItem.AllowUploadCustomImage, | ||
]); | ||
}); | ||
|
||
it("workspace features can be cleared again", () => { | ||
const { result, rerender } = getFeatures({ | ||
workspace: { [FeatureItem.AllowCustomDBT]: true, [FeatureItem.AllowSync]: false } as FeatureSet, | ||
}); | ||
expect(result.current.sort()).toEqual([FeatureItem.AllowCreateConnection, FeatureItem.AllowCustomDBT]); | ||
rerender({ workspace: undefined }); | ||
expect(result.current.sort()).toEqual([FeatureItem.AllowCreateConnection, FeatureItem.AllowSync]); | ||
}); | ||
|
||
it("user features can be cleared again", () => { | ||
const { result, rerender } = getFeatures({ | ||
user: { [FeatureItem.AllowCustomDBT]: true, [FeatureItem.AllowSync]: false } as FeatureSet, | ||
}); | ||
expect(result.current.sort()).toEqual([FeatureItem.AllowCreateConnection, FeatureItem.AllowCustomDBT]); | ||
rerender({ user: undefined }); | ||
expect(result.current.sort()).toEqual([FeatureItem.AllowCreateConnection, FeatureItem.AllowSync]); | ||
}); | ||
|
||
it("overwritten features can be cleared again", () => { | ||
const { result, rerender } = getFeatures({ | ||
overwrite: { [FeatureItem.AllowCustomDBT]: true, [FeatureItem.AllowSync]: false } as FeatureSet, | ||
}); | ||
expect(result.current.sort()).toEqual([FeatureItem.AllowCreateConnection, FeatureItem.AllowCustomDBT]); | ||
rerender({ overwrite: undefined }); | ||
expect(result.current.sort()).toEqual([FeatureItem.AllowCreateConnection, FeatureItem.AllowSync]); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("useFeatureRegisterValues", () => { | ||
test("should register more than 1 feature", async () => { | ||
const { result } = renderHook( | ||
() => { | ||
useFeatureRegisterValues([{ id: FeatureItem.AllowCreateConnection }]); | ||
useFeatureRegisterValues([{ id: FeatureItem.AllowSync }]); | ||
|
||
return useFeatureService(); | ||
}, | ||
{ | ||
initialProps: { initialValue: 0 }, | ||
wrapper, | ||
} | ||
); | ||
|
||
expect(result.current.features).toEqual([ | ||
...predefinedFeatures, | ||
{ id: FeatureItem.AllowCreateConnection }, | ||
{ id: FeatureItem.AllowSync }, | ||
]); | ||
|
||
act(() => { | ||
result.current.unregisterFeature([FeatureItem.AllowCreateConnection]); | ||
describe("IfFeatureEnabled", () => { | ||
it("renders its children if the given feature is enabled", () => { | ||
const { getByTestId } = render( | ||
<IfFeatureEnabled feature={FeatureItem.AllowCreateConnection}> | ||
<span data-testid="content" /> | ||
</IfFeatureEnabled>, | ||
{ wrapper } | ||
); | ||
expect(getByTestId("content")).toBeTruthy(); | ||
}); | ||
|
||
expect(result.current.features).toEqual([...predefinedFeatures, { id: FeatureItem.AllowSync }]); | ||
it("does not render its children if the given feature is disabled", () => { | ||
const { queryByTestId } = render( | ||
<IfFeatureEnabled feature={FeatureItem.AllowOAuthConnector}> | ||
<span data-testid="content" /> | ||
</IfFeatureEnabled>, | ||
{ wrapper } | ||
); | ||
expect(queryByTestId("content")).toBeFalsy(); | ||
}); | ||
|
||
it("allows changing features and rerenders correctly", () => { | ||
const { queryByTestId, rerender } = render( | ||
<FeatureService features={[FeatureItem.AllowCreateConnection]}> | ||
<IfFeatureEnabled feature={FeatureItem.AllowOAuthConnector}> | ||
<span data-testid="content" /> | ||
</IfFeatureEnabled> | ||
</FeatureService> | ||
); | ||
expect(queryByTestId("content")).toBeFalsy(); | ||
rerender( | ||
<FeatureService features={[FeatureItem.AllowOAuthConnector]}> | ||
<IfFeatureEnabled feature={FeatureItem.AllowOAuthConnector}> | ||
<span data-testid="content" /> | ||
</IfFeatureEnabled> | ||
</FeatureService> | ||
); | ||
expect(queryByTestId("content")).toBeTruthy(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be better to have the features in a constant such as
defaultFeatures
so that it can be referenced later in the tests.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally would prefer keeping it that way. Most of the tests tests different combinations of enabling/disabling, why we can't spread that anyway simply into it. Also given that those tests all test whether overwriting etc work, I'd prefer if someone ever adjust this line, manually also needs to adjust the expect cases below, to sanity check that the overwrite logic is really working as expected.