|
| 1 | +import { QueryClient } from "react-query"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import type { |
| 4 | + UpdateUserAppearanceSettingsRequest, |
| 5 | + UserAppearanceSettings, |
| 6 | +} from "#/api/typesGenerated"; |
| 7 | +import { myAppearanceKey, updateAppearanceSettings } from "./users"; |
| 8 | + |
| 9 | +const appearanceSettings = ( |
| 10 | + overrides: Partial<UserAppearanceSettings> = {}, |
| 11 | +): UserAppearanceSettings => ({ |
| 12 | + theme_preference: "dark-tritan", |
| 13 | + theme_mode: "sync", |
| 14 | + theme_light: "light-tritan", |
| 15 | + theme_dark: "dark-tritan", |
| 16 | + terminal_font: "geist-mono", |
| 17 | + ...overrides, |
| 18 | +}); |
| 19 | + |
| 20 | +const updateRequest = ( |
| 21 | + overrides: Partial<UpdateUserAppearanceSettingsRequest> = {}, |
| 22 | +): UpdateUserAppearanceSettingsRequest => ({ |
| 23 | + theme_preference: "dark", |
| 24 | + theme_mode: "single", |
| 25 | + theme_light: "light-tritan", |
| 26 | + theme_dark: "dark-tritan", |
| 27 | + terminal_font: "fira-code", |
| 28 | + ...overrides, |
| 29 | +}); |
| 30 | + |
| 31 | +describe("updateAppearanceSettings", () => { |
| 32 | + it("rolls back optimistic appearance updates when the mutation fails", async () => { |
| 33 | + const queryClient = new QueryClient(); |
| 34 | + const previousSettings = appearanceSettings({ |
| 35 | + theme_light: "light-protan-deuter", |
| 36 | + theme_dark: "dark-protan-deuter", |
| 37 | + }); |
| 38 | + const optimisticSettings = updateRequest(); |
| 39 | + |
| 40 | + queryClient.setQueryData<UserAppearanceSettings>( |
| 41 | + myAppearanceKey, |
| 42 | + previousSettings, |
| 43 | + ); |
| 44 | + |
| 45 | + const mutation = updateAppearanceSettings(queryClient); |
| 46 | + const context = await mutation.onMutate?.(optimisticSettings); |
| 47 | + expect(queryClient.getQueryData(myAppearanceKey)).toEqual( |
| 48 | + optimisticSettings, |
| 49 | + ); |
| 50 | + |
| 51 | + mutation.onError?.(new Error("failed"), optimisticSettings, context); |
| 52 | + |
| 53 | + expect(queryClient.getQueryData(myAppearanceKey)).toEqual(previousSettings); |
| 54 | + }); |
| 55 | + |
| 56 | + it("removes optimistic appearance data when rollback has no prior cache", async () => { |
| 57 | + const queryClient = new QueryClient(); |
| 58 | + const optimisticSettings = updateRequest(); |
| 59 | + const mutation = updateAppearanceSettings(queryClient); |
| 60 | + |
| 61 | + const context = await mutation.onMutate?.(optimisticSettings); |
| 62 | + expect(queryClient.getQueryData(myAppearanceKey)).toEqual( |
| 63 | + optimisticSettings, |
| 64 | + ); |
| 65 | + |
| 66 | + mutation.onError?.(new Error("failed"), optimisticSettings, context); |
| 67 | + |
| 68 | + expect(queryClient.getQueryData(myAppearanceKey)).toBeUndefined(); |
| 69 | + }); |
| 70 | + |
| 71 | + it("stores the server response after a successful appearance update", async () => { |
| 72 | + const queryClient = new QueryClient(); |
| 73 | + const optimisticSettings = updateRequest(); |
| 74 | + const serverSettings = appearanceSettings({ |
| 75 | + theme_preference: "dark-protan-deuter", |
| 76 | + theme_light: "light-protan-deuter", |
| 77 | + theme_dark: "dark-protan-deuter", |
| 78 | + }); |
| 79 | + const mutation = updateAppearanceSettings(queryClient); |
| 80 | + |
| 81 | + const context = await mutation.onMutate?.(optimisticSettings); |
| 82 | + if (!context) { |
| 83 | + throw new Error("expected mutation context"); |
| 84 | + } |
| 85 | + expect(queryClient.getQueryData(myAppearanceKey)).toEqual( |
| 86 | + optimisticSettings, |
| 87 | + ); |
| 88 | + |
| 89 | + mutation.onSuccess?.(serverSettings, optimisticSettings, context); |
| 90 | + |
| 91 | + expect(queryClient.getQueryData(myAppearanceKey)).toEqual(serverSettings); |
| 92 | + }); |
| 93 | + |
| 94 | + it("keeps patch values when a successful appearance update response is partial", async () => { |
| 95 | + const queryClient = new QueryClient(); |
| 96 | + const optimisticSettings = updateRequest({ |
| 97 | + theme_mode: "sync", |
| 98 | + theme_light: "light-protan-deuter", |
| 99 | + theme_dark: "dark-protan-deuter", |
| 100 | + }); |
| 101 | + const serverSettings = { |
| 102 | + theme_preference: "dark-tritan", |
| 103 | + terminal_font: "jetbrains-mono", |
| 104 | + } satisfies Partial<UserAppearanceSettings>; |
| 105 | + const mutation = updateAppearanceSettings(queryClient); |
| 106 | + |
| 107 | + const context = await mutation.onMutate?.(optimisticSettings); |
| 108 | + if (!context) { |
| 109 | + throw new Error("expected mutation context"); |
| 110 | + } |
| 111 | + |
| 112 | + mutation.onSuccess?.( |
| 113 | + serverSettings as UserAppearanceSettings, |
| 114 | + optimisticSettings, |
| 115 | + context, |
| 116 | + ); |
| 117 | + |
| 118 | + expect(queryClient.getQueryData(myAppearanceKey)).toEqual({ |
| 119 | + ...optimisticSettings, |
| 120 | + ...serverSettings, |
| 121 | + }); |
| 122 | + }); |
| 123 | +}); |
0 commit comments