mirror of
https://git.shipoclu.com/moon/plspace.git
synced 2026-08-13 02:42:30 +00:00
improved component composition and added tests.
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
import { ApiClient } from '$lib/api/client'
|
||||
import type { Account, Status } from '$lib/api/types'
|
||||
import {
|
||||
createAppServices,
|
||||
type AppServices,
|
||||
type AppServiceOverrides,
|
||||
type SessionService,
|
||||
type ThemeService,
|
||||
} from '$lib/app-services'
|
||||
|
||||
export function account(overrides: Partial<Account> = {}): Account {
|
||||
return {
|
||||
id: 'account-1',
|
||||
username: 'alice',
|
||||
acct: 'alice',
|
||||
display_name: 'Alice',
|
||||
note: '',
|
||||
url: 'https://example.test/@alice',
|
||||
avatar: '',
|
||||
avatar_static: '',
|
||||
header: '',
|
||||
header_static: '',
|
||||
locked: false,
|
||||
created_at: '2020-01-01T00:00:00.000Z',
|
||||
statuses_count: 0,
|
||||
followers_count: 0,
|
||||
following_count: 0,
|
||||
fields: [],
|
||||
emojis: [],
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
export function status(overrides: Partial<Status> = {}): Status {
|
||||
return {
|
||||
id: 'status-1',
|
||||
uri: 'https://example.test/statuses/1',
|
||||
created_at: '2026-01-01T00:00:00.000Z',
|
||||
account: account(),
|
||||
content: '<p>Hello</p>',
|
||||
visibility: 'public',
|
||||
sensitive: false,
|
||||
spoiler_text: '',
|
||||
in_reply_to_id: null,
|
||||
in_reply_to_account_id: null,
|
||||
replies_count: 0,
|
||||
reblogs_count: 0,
|
||||
favourites_count: 0,
|
||||
media_attachments: [],
|
||||
mentions: [],
|
||||
tags: [],
|
||||
emojis: [],
|
||||
reblog: null,
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
export function session(overrides: Partial<SessionService> = {}): SessionService {
|
||||
const host = overrides.host ?? 'example.test'
|
||||
return {
|
||||
host,
|
||||
token: null,
|
||||
me: null,
|
||||
instance: null,
|
||||
loading: false,
|
||||
error: null,
|
||||
api: new ApiClient(host),
|
||||
signedIn: false,
|
||||
connected: Boolean(host),
|
||||
restore: async () => null,
|
||||
connect: async () => {},
|
||||
login: async () => {},
|
||||
logout: async () => {},
|
||||
disconnect: () => {},
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
export function theme(overrides: Partial<ThemeService> = {}): ThemeService {
|
||||
return {
|
||||
viewerCss: '',
|
||||
allowProfileCss: true,
|
||||
setViewerCss: () => {},
|
||||
setAllowProfileCss: () => {},
|
||||
applyProfileCss: () => {},
|
||||
clearProfileCss: () => {},
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test services fail fast on any endpoint that was not explicitly faked. This
|
||||
* turns an accidental network request into a local, descriptive test failure.
|
||||
*/
|
||||
export function testServices(overrides: AppServiceOverrides = {}): AppServices {
|
||||
const services = createAppServices({
|
||||
...overrides,
|
||||
session: overrides.session ?? session(),
|
||||
})
|
||||
const configured = overrides.endpoints ?? {}
|
||||
services.endpoints = new Proxy(services.endpoints, {
|
||||
get(target, property, receiver) {
|
||||
if (property in configured) return Reflect.get(target, property, receiver)
|
||||
const value = Reflect.get(target, property, receiver)
|
||||
if (typeof value !== 'function') return value
|
||||
return () => {
|
||||
throw new Error(`Test attempted an unfaked endpoint: ${String(property)}`)
|
||||
}
|
||||
},
|
||||
})
|
||||
return services
|
||||
}
|
||||
|
||||
export function deferred<T>(): {
|
||||
promise: Promise<T>
|
||||
resolve(value: T): void
|
||||
reject(cause: unknown): void
|
||||
} {
|
||||
let resolve!: (value: T) => void
|
||||
let reject!: (cause: unknown) => void
|
||||
const promise = new Promise<T>((res, rej) => {
|
||||
resolve = res
|
||||
reject = rej
|
||||
})
|
||||
return { promise, resolve, reject }
|
||||
}
|
||||
Reference in New Issue
Block a user