Skip to content

Commit

Permalink
test: Add TypeScript tests for lifecycle handler types.
Browse files Browse the repository at this point in the history
  • Loading branch information
marceliwac committed Sep 14, 2024
1 parent 51704e6 commit efb777c
Showing 1 changed file with 75 additions and 2 deletions.
77 changes: 75 additions & 2 deletions test/typescript/tsd/ServiceSchema_lifecycles.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { expectType } from "tsd";
import { Service, ServiceBroker, ServiceSchema } from "../../../index";
import {expectAssignable, expectType} from "tsd";
import {
Service, ServiceAsyncLifecycleHandler,
ServiceBroker,
ServiceSchema,
ServiceSettingSchema,
ServiceSyncLifecycleHandler
} from "../../../index";

const broker = new ServiceBroker({ logger: false, transporter: "fake" });

Expand Down Expand Up @@ -42,6 +48,39 @@ class TestService3 extends Service {
}
}

interface TestService4SettingSchema {
testService4Setting: string;
}

const testService4Schema: ServiceSchema<TestService4SettingSchema> = {
name: "test4",
settings: {
testService4Setting: "testService4"
},
}

interface TestService5Methods {
testService5Method: () => void;
}


interface TestService5SettingSchema {
testService5Setting: string;
}

interface TestService5This extends Service<TestService5SettingSchema>, TestService5Methods {}

const testService5Schema: ServiceSchema<TestService5SettingSchema, TestService5This> = {
name: "test5",
settings: {
testService5Setting: "testService5"
},
methods: {
testsService5Method: () => {},
}
}


const testService1 = new TestService1(broker);
expectType<ServiceSchema>(testService1.schema);

Expand All @@ -50,3 +89,37 @@ expectType<ServiceSchema>(testService2.schema);

const testService3 = new TestService3(broker);
expectType<ServiceSchema>(testService3.schema);

// Ensure that the lifecycle handlers are typed correctly when "This" type is not provided to service schema type
expectType<
ServiceSyncLifecycleHandler<Service<TestService4SettingSchema>> |
ServiceSyncLifecycleHandler<Service<TestService4SettingSchema>>[] |
undefined
>(testService4Schema.created)
expectType<
ServiceAsyncLifecycleHandler<Service<TestService4SettingSchema>> |
ServiceAsyncLifecycleHandler<Service<TestService4SettingSchema>>[] |
undefined
>(testService4Schema.started)
expectType<
ServiceAsyncLifecycleHandler<Service<TestService4SettingSchema>> |
ServiceAsyncLifecycleHandler<Service<TestService4SettingSchema>>[] |
undefined
>(testService4Schema.stopped)

// Ensure that the lifecycle handlers are typed correctly when "This" type is provided to service schema type
expectType<
ServiceSyncLifecycleHandler<TestService5This> |
ServiceSyncLifecycleHandler<TestService5This>[] |
undefined
>(testService5Schema.created)
expectType<
ServiceAsyncLifecycleHandler<TestService5This> |
ServiceAsyncLifecycleHandler<TestService5This>[] |
undefined
>(testService5Schema.started)
expectType<
ServiceAsyncLifecycleHandler<TestService5This> |
ServiceAsyncLifecycleHandler<TestService5This>[] |
undefined
>(testService5Schema.stopped)

0 comments on commit efb777c

Please sign in to comment.