|
| 1 | +import { describe, expect, it } from "vitest" |
| 2 | +import { ClientError, Status } from "nice-grpc-common" |
| 3 | +import type { User } from "$lib/server/grpc/generated/user/entities/user" |
| 4 | +import { |
| 5 | + editRequestFor, |
| 6 | + failForLinks, |
| 7 | + profileLinksFrom, |
| 8 | + profileLinksOf, |
| 9 | +} from "./profileLinks" |
| 10 | + |
| 11 | +const formOf = (fields: Record<string, string>) => { |
| 12 | + const form = new FormData() |
| 13 | + for (const [name, value] of Object.entries(fields)) form.set(name, value) |
| 14 | + return form |
| 15 | +} |
| 16 | + |
| 17 | +const grpcError = (code: Status) => |
| 18 | + new ClientError("/user.UserService/Edit", code, "invalid GitHub URL 'x'") |
| 19 | + |
| 20 | +describe("profileLinksOf", () => { |
| 21 | + it("reads the rendered URLs off the user", () => { |
| 22 | + const user = { |
| 23 | + githubUrl: "https://github.com/octo", |
| 24 | + linkedinUrl: "", |
| 25 | + renkuUrl: "https://renkulab.io/u/jane", |
| 26 | + } as User |
| 27 | + |
| 28 | + expect(profileLinksOf(user)).toEqual({ |
| 29 | + github: "https://github.com/octo", |
| 30 | + linkedin: "", |
| 31 | + renku: "https://renkulab.io/u/jane", |
| 32 | + }) |
| 33 | + }) |
| 34 | + |
| 35 | + // No platform user when the backend was down for WhoAmI. |
| 36 | + it("gives empty links for no user", () => { |
| 37 | + expect(profileLinksOf(undefined)).toEqual({ |
| 38 | + github: "", |
| 39 | + linkedin: "", |
| 40 | + renku: "", |
| 41 | + }) |
| 42 | + }) |
| 43 | +}) |
| 44 | + |
| 45 | +describe("profileLinksFrom", () => { |
| 46 | + it("trims what was typed", () => { |
| 47 | + const form = formOf({ |
| 48 | + github: " https://github.com/octo ", |
| 49 | + linkedin: "", |
| 50 | + renku: "\thttps://renkulab.io/u/jane\n", |
| 51 | + }) |
| 52 | + |
| 53 | + expect(profileLinksFrom(form)).toEqual({ |
| 54 | + github: "https://github.com/octo", |
| 55 | + linkedin: "", |
| 56 | + renku: "https://renkulab.io/u/jane", |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + // The distinction the backend acts on: "" removes, undefined keeps. |
| 61 | + it("leaves a field the form did not post undefined, not empty", () => { |
| 62 | + const links = profileLinksFrom(formOf({ github: "" })) |
| 63 | + |
| 64 | + expect(links.github).toBe("") |
| 65 | + expect(links.linkedin).toBeUndefined() |
| 66 | + expect(links.renku).toBeUndefined() |
| 67 | + }) |
| 68 | +}) |
| 69 | + |
| 70 | +describe("editRequestFor", () => { |
| 71 | + it("sends an emptied box as an empty string, which removes the link", () => { |
| 72 | + expect( |
| 73 | + editRequestFor("u-1", { |
| 74 | + github: "", |
| 75 | + linkedin: "https://linkedin.com/in/jane", |
| 76 | + renku: "", |
| 77 | + }), |
| 78 | + ).toEqual({ |
| 79 | + userId: "u-1", |
| 80 | + githubUrl: "", |
| 81 | + linkedinUrl: "https://linkedin.com/in/jane", |
| 82 | + renkuUrl: "", |
| 83 | + }) |
| 84 | + }) |
| 85 | + |
| 86 | + it("leaves out a link it was not given, which keeps it", () => { |
| 87 | + const req = editRequestFor("u-1", { github: "https://github.com/octo" }) |
| 88 | + |
| 89 | + expect(req.linkedinUrl).toBeUndefined() |
| 90 | + expect(req.renkuUrl).toBeUndefined() |
| 91 | + }) |
| 92 | +}) |
| 93 | + |
| 94 | +describe("failForLinks", () => { |
| 95 | + const values = { github: "x" } |
| 96 | + |
| 97 | + it("passes the backend's reason through for an invalid link", () => { |
| 98 | + const failure = failForLinks(grpcError(Status.INVALID_ARGUMENT), values) |
| 99 | + |
| 100 | + expect(failure.status).toBe(400) |
| 101 | + expect(failure.data).toEqual({ |
| 102 | + message: "invalid GitHub URL 'x'", |
| 103 | + values, |
| 104 | + }) |
| 105 | + }) |
| 106 | + |
| 107 | + it.each([ |
| 108 | + [Status.PERMISSION_DENIED, 403], |
| 109 | + [Status.NOT_FOUND, 404], |
| 110 | + ])("maps gRPC %s to %s and keeps the typed values", (code, status) => { |
| 111 | + const failure = failForLinks(grpcError(code), values) |
| 112 | + |
| 113 | + expect(failure.status).toBe(status) |
| 114 | + expect(failure.data.values).toBe(values) |
| 115 | + }) |
| 116 | + |
| 117 | + // Anything else is not the user's to fix, so it goes to handleError. |
| 118 | + it("rethrows other errors", () => { |
| 119 | + const unavailable = grpcError(Status.UNAVAILABLE) |
| 120 | + const plain = new Error("boom") |
| 121 | + |
| 122 | + expect(() => failForLinks(unavailable, values)).toThrow(unavailable) |
| 123 | + expect(() => failForLinks(plain, values)).toThrow(plain) |
| 124 | + }) |
| 125 | +}) |
0 commit comments