Repository navigation
fix: Vue Integration (fixes #68) #233
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| { | ||
| "name": "@tryabby/vue", | ||
| "version": "0.0.1", | ||
| "description": "Vue integration for Abby", | ||
| "main": "./dist/index.js", | ||
| "module": "./dist/index.mjs", | ||
| "types": "./dist/index.d.ts", | ||
| "files": [ | ||
| "dist" | ||
| ], | ||
| "scripts": { | ||
| "build": "tsup src/index.ts --format cjs,esm --dts", | ||
| "dev": "tsup src/index.ts --format cjs,esm --watch --dts", | ||
| "lint": "biome lint .", | ||
| "test": "vitest run", | ||
| "test:watch": "vitest" | ||
| }, | ||
| "dependencies": { | ||
| "@tryabby/core": "workspace:*", | ||
| "vue": "^3.3.0" | ||
| }, | ||
|
Comment on lines
+18
to
+21
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Declare
|
||
| "devDependencies": { | ||
| "@vue/test-utils": "^2.4.1", | ||
| "jsdom": "^22.1.0", | ||
| "tsup": "^7.2.0", | ||
| "typescript": "^5.2.2", | ||
| "vitest": "^0.34.4" | ||
| }, | ||
| "publishConfig": { | ||
| "access": "public" | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import { Abby, type AbbyConfig, type RemoteConfigValueStringToType } from "@tryabby/core"; | ||
| import { inject, provide, ref, onMounted, type InjectionKey, type Ref } from "vue"; | ||
|
|
||
| const ABBY_SYMBOL: InjectionKey<Abby<any, any, any, any>> = Symbol("abby"); | ||
|
|
||
| export function createAbby< | ||
| TKey extends string = string, | ||
| TTests extends Record<string, { variants: readonly string[] }> = Record<string, { variants: readonly string[] }>, | ||
| TFlags extends readonly string[] = readonly string[], | ||
| TRemoteConfig extends Record<string, "String" | "Number" | "JSON"> = Record<string, "String" | "Number" | "JSON"> | ||
| >(config: AbbyConfig<TKey, TTests, TFlags, TRemoteConfig>) { | ||
| const abby = new Abby(config); | ||
|
Comment on lines
+11
to
+12
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generic arguments to
These mismatches are type errors at the parameter declaration. Pass the generics in the declared order. Add an AbbyConfig<TKey, TTests, TEnvs, keyof TRemoteConfig & string, TRemoteConfig>Update the
|
||
|
|
||
| const AbbyProvider = { | ||
| name: "AbbyProvider", | ||
| setup(_props: any, { slots }: any) { | ||
| provide(ABBY_SYMBOL, abby); | ||
| return () => slots.default?.(); | ||
| }, | ||
| }; | ||
|
|
||
| function useAbby<T extends keyof TTests>( | ||
| testName: T, | ||
| lookup?: Record<TTests[T]["variants"][number], any> | ||
| ) { | ||
| const abbyInstance = inject(ABBY_SYMBOL); | ||
| if (!abbyInstance) { | ||
| throw new Error("useAbby must be used within an AbbyProvider"); | ||
| } | ||
|
|
||
| const variant = ref(abbyInstance.getTestVariant(testName as string)); | ||
|
|
||
| if (lookup) { | ||
| variant.value = lookup[variant.value as TTests[T]["variants"][number]]; | ||
| } | ||
|
Comment on lines
+31
to
+35
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Vue composables never load Abby project data, so flags and remote config stay at defaults.
Load the project data in the integration, then update the refs when Abby notifies its listeners. The React integration does this with
|
||
|
|
||
| onMounted(() => { | ||
| // Abby core handles the pinging and persistence | ||
| }); | ||
|
|
||
| const onAct = () => { | ||
| abbyInstance.onAct(testName as string); | ||
| }; | ||
|
Comment on lines
+41
to
+43
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The Renaming the call does not fix this. The
|
||
|
|
||
| return { | ||
| variant: variant as Ref<typeof lookup extends undefined ? TTests[T]["variants"][number] : typeof lookup[TTests[T]["variants"][number]]>, | ||
| onAct, | ||
| }; | ||
| } | ||
|
|
||
| function useFeatureFlag<F extends TFlags[number]>(flagName: F): Ref<boolean> { | ||
| const abbyInstance = inject(ABBY_SYMBOL); | ||
| if (!abbyInstance) { | ||
| throw new Error("useFeatureFlag must be used within an AbbyProvider"); | ||
| } | ||
|
|
||
| const isEnabled = ref(abbyInstance.getFeatureFlag(flagName)); | ||
|
|
||
| return isEnabled; | ||
| } | ||
|
|
||
| function useRemoteConfig<R extends keyof TRemoteConfig>( | ||
| remoteConfigName: R | ||
| ): Ref<RemoteConfigValueStringToType<TRemoteConfig[R]>> { | ||
| const abbyInstance = inject(ABBY_SYMBOL); | ||
| if (!abbyInstance) { | ||
| throw new Error("useRemoteConfig must be used within an AbbyProvider"); | ||
| } | ||
|
|
||
| const value = ref(abbyInstance.getRemoteConfig(remoteConfigName as string)); | ||
|
|
||
| return value as Ref<RemoteConfigValueStringToType<TRemoteConfig[R]>>; | ||
| } | ||
|
|
||
| return { | ||
| AbbyProvider, | ||
| useAbby, | ||
| useFeatureFlag, | ||
| useRemoteConfig, | ||
| getVariants: abby.getVariants.bind(abby), | ||
| getFeatureFlagValue: abby.getFeatureFlag.bind(abby), | ||
| getABTestValue: abby.getTestVariant.bind(abby), | ||
| getRemoteConfigValue: abby.getRemoteConfig.bind(abby), | ||
| updateUserProperties: abby.updateUserProperties.bind(abby), | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { describe, it, expectTypeOf } from "vitest"; | ||
| import { createAbby } from "../src"; | ||
| import { type Ref } from "vue"; | ||
|
|
||
| describe("Vue Integration Types", () => { | ||
| it("has correct types for useAbby", () => { | ||
| const { useAbby } = createAbby({ | ||
| environments: ["dev"], | ||
| currentEnvironment: "dev", | ||
| projectId: "test", | ||
| tests: { | ||
| test1: { variants: ["A", "B"] as const }, | ||
| }, | ||
| }); | ||
|
|
||
| // This is a bit tricky to test without a full vue environment but we can check the return types | ||
| // In a real test we'd need to mock inject | ||
| }); | ||
|
|
||
| it("has correct types for getVariants", () => { | ||
| const { getVariants } = createAbby({ | ||
| environments: ["dev"], | ||
| currentEnvironment: "dev", | ||
| projectId: "test", | ||
| tests: { | ||
| test1: { variants: ["A", "B"] as const }, | ||
| }, | ||
| }); | ||
|
|
||
| expectTypeOf(getVariants("test1")).toEqualTypeOf<readonly ["A", "B"]>(); | ||
| }); | ||
|
|
||
| it("has correct types for useFeatureFlag", () => { | ||
| const { useFeatureFlag } = createAbby({ | ||
| environments: ["dev"], | ||
| currentEnvironment: "dev", | ||
| projectId: "test", | ||
| flags: ["flag1"] as const, | ||
| }); | ||
|
|
||
| // @ts-expect-error - flag2 does not exist | ||
| () => useFeatureFlag("flag2"); | ||
| }); | ||
|
|
||
| it("has correct types for useRemoteConfig", () => { | ||
| const { useRemoteConfig } = createAbby({ | ||
| environments: ["dev"], | ||
| currentEnvironment: "dev", | ||
| projectId: "test", | ||
| remoteConfig: { | ||
| rc1: "String", | ||
| rc2: "Number", | ||
| rc3: "JSON", | ||
| } as const, | ||
| }); | ||
|
|
||
| // @ts-expect-error - rc4 does not exist | ||
| () => useRemoteConfig("rc4"); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { mount } from "@vue/test-utils"; | ||
| import { createAbby } from "../src"; | ||
| import { defineComponent, h } from "vue"; | ||
| import { HttpService } from "@tryabby/core"; | ||
|
|
||
| vi.mock("@tryabby/core", async () => { | ||
| const actual = await vi.importActual("@tryabby/core") as any; | ||
| return { | ||
| ...actual, | ||
| HttpService: { | ||
| sendData: vi.fn().mockResolvedValue({}), | ||
| fetchData: vi.fn().mockResolvedValue({}), | ||
| }, | ||
| }; | ||
| }); | ||
|
Comment on lines
+5
to
+16
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the unused The Do not fix this by spreading the real class. Class static methods are non-enumerable, so Suggested fix for the unused import
|
||
|
|
||
| describe("Vue Integration", () => { | ||
| const config = { | ||
| environments: ["dev"], | ||
| currentEnvironment: "dev", | ||
| projectId: "test-project", | ||
| tests: { | ||
| "test-1": { variants: ["A", "B"] }, | ||
| }, | ||
| flags: ["flag-1"] as const, | ||
| remoteConfig: { | ||
| "config-1": "String", | ||
| } as const, | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("provides and consumes abby instance", async () => { | ||
| const { AbbyProvider, useAbby } = createAbby(config); | ||
|
|
||
| const Consumer = defineComponent({ | ||
| setup() { | ||
| const { variant, onAct } = useAbby("test-1"); | ||
| return { variant, onAct }; | ||
| }, | ||
| template: "<div>{{ variant }}</div>", | ||
| }); | ||
|
|
||
| const App = defineComponent({ | ||
| render() { | ||
| return h(AbbyProvider, {}, { default: () => h(Consumer) }); | ||
| }, | ||
| }); | ||
|
|
||
| const wrapper = mount(App); | ||
| expect(["A", "B"]).toContain(wrapper.text()); | ||
| }); | ||
|
|
||
| it("useFeatureFlag returns a boolean ref", () => { | ||
| const { AbbyProvider, useFeatureFlag } = createAbby(config); | ||
|
|
||
| const Consumer = defineComponent({ | ||
| setup() { | ||
| const isEnabled = useFeatureFlag("flag-1"); | ||
| return { isEnabled }; | ||
| }, | ||
| template: "<div>{{ isEnabled }}</div>", | ||
| }); | ||
|
|
||
| const App = defineComponent({ | ||
| render() { | ||
| return h(AbbyProvider, {}, { default: () => h(Consumer) }); | ||
| }, | ||
| }); | ||
|
|
||
| const wrapper = mount(App); | ||
| expect(["true", "false"]).toContain(wrapper.text()); | ||
| }); | ||
|
|
||
| it("useRemoteConfig returns the config value", () => { | ||
| const { AbbyProvider, useRemoteConfig } = createAbby(config); | ||
|
|
||
| const Consumer = defineComponent({ | ||
| setup() { | ||
| const configValue = useRemoteConfig("config-1"); | ||
| return { configValue }; | ||
| }, | ||
| template: "<div>{{ configValue }}</div>", | ||
| }); | ||
|
|
||
| const App = defineComponent({ | ||
| render() { | ||
| return h(AbbyProvider, {}, { default: () => h(Consumer) }); | ||
| }, | ||
| }); | ||
|
|
||
| const wrapper = mount(App); | ||
| // Default value for String remote config in core seems to be empty string or something if not fetched | ||
| // But here it should return something. | ||
| expect(wrapper.text()).toBeDefined(); | ||
| }); | ||
|
|
||
| it("useAbby with lookup works", () => { | ||
| const { AbbyProvider, useAbby } = createAbby(config); | ||
|
|
||
| const Consumer = defineComponent({ | ||
| setup() { | ||
| const { variant } = useAbby("test-1", { A: "Variant A", B: "Variant B" }); | ||
| return { variant }; | ||
| }, | ||
| template: "<div>{{ variant }}</div>", | ||
| }); | ||
|
|
||
| const App = defineComponent({ | ||
| render() { | ||
| return h(AbbyProvider, {}, { default: () => h(Consumer) }); | ||
| }, | ||
| }); | ||
|
|
||
| const wrapper = mount(App); | ||
| expect(["Variant A", "Variant B"]).toContain(wrapper.text()); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,23 @@ | ||||
| { | ||||
| "extends": "../../tsconfig.json", | ||||
| "compilerOptions": { | ||||
| "outDir": "./dist", | ||||
| "rootDir": "./src", | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove
Suggested fix
If Committable suggestion
|
||||
| "composite": false, | ||||
| "declaration": true, | ||||
| "declarationMap": true, | ||||
| "sourceMap": true, | ||||
| "module": "ESNext", | ||||
| "moduleResolution": "Bundler", | ||||
| "target": "ESNext", | ||||
| "lib": ["ESNext", "DOM"], | ||||
| "strict": true, | ||||
| "esModuleInterop": true, | ||||
| "skipLibCheck": true, | ||||
| "forceConsistentCasingInFileNames": true, | ||||
| "jsx": "preserve", | ||||
| "types": ["vitest/globals"] | ||||
| }, | ||||
| "include": ["src/**/*", "tests/**/*"], | ||||
| "exclude": ["node_modules", "dist"] | ||||
| } | ||||
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.
Enable a DOM environment for the Vue integration tests.
vitest runinpackages/vuehas no Vitest config and noenvironmentsetting. Vitest therefore runs in the defaultnodeenvironment, even thoughjsdomis listed as a devDependency. The fourmountcalls intests/vue-integration.test.tsneeddocument, so each test fails with a missing-documenterror. Thetestscript does not pass as written.Select
jsdomfor this test file with a Vitest environment pragma:Suggested fix
+// @vitest-environment jsdom import { describe, it, expect, vi, beforeEach } from "vitest"; import { mount } from "@vue/test-utils";