diff --git a/packages/types/package.json b/packages/types/package.json index 8274c1ce74..e8117fff37 100644 --- a/packages/types/package.json +++ b/packages/types/package.json @@ -31,7 +31,9 @@ }, "dependencies": {}, "devDependencies": { - "@backstage/cli": "^0.7.11" + "@backstage/cli": "^0.7.11", + "@types/zen-observable": "^0.8.0", + "zen-observable": "^0.8.15" }, "files": [ "dist" diff --git a/packages/types/src/json.test.ts b/packages/types/src/json.test.ts new file mode 100644 index 0000000000..7da0ef8a3e --- /dev/null +++ b/packages/types/src/json.test.ts @@ -0,0 +1,81 @@ +/* + * Copyright 2021 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { JsonPrimitive, JsonArray, JsonObject, JsonValue } from './json'; + +describe('json', () => { + it('JsonPrimitive', () => { + function isValid(..._v: JsonPrimitive[]) {} + isValid(1, 's', true, false, null); + + // @ts-expect-error + const v1: JsonPrimitive = []; + // @ts-expect-error + const v2: JsonPrimitive = {}; + + expect(true).toBe(true); + }); + + it('JsonArray', () => { + function isValid(..._v: JsonArray[]) {} + isValid([], [1, 's', true, false, null, {}, []]); + + // @ts-expect-error + const v1: JsonArray = 1; + // @ts-expect-error + const v2: JsonArray = 's'; + // @ts-expect-error + const v3: JsonArray = true; + // @ts-expect-error + const v4: JsonArray = false; + // @ts-expect-error + const v5: JsonArray = null; + // @ts-expect-error + const v6: JsonArray = {}; + + expect(true).toBe(true); + }); + + it('JsonObject', () => { + function isValid(..._v: JsonObject[]) {} + isValid( + {}, + { v1: 1, v2: 's', v3: true, v4: false, v5: null, v6: {}, v7: [] }, + ); + + // @ts-expect-error + const v1: JsonObject = 1; + // @ts-expect-error + const v2: JsonObject = 's'; + // @ts-expect-error + const v3: JsonObject = true; + // @ts-expect-error + const v4: JsonObject = false; + // @ts-expect-error + const v5: JsonObject = null; + // @ts-expect-error + const v6: JsonObject = []; + + expect(true).toBe(true); + }); + + it('JsonValue', () => { + function isValid(..._v: JsonValue[]) {} + isValid(1, 's', true, false, null, {}, []); + + expect(true).toBe(true); + }); +}); diff --git a/packages/types/src/observable.test.ts b/packages/types/src/observable.test.ts new file mode 100644 index 0000000000..77cd074fa3 --- /dev/null +++ b/packages/types/src/observable.test.ts @@ -0,0 +1,42 @@ +/* + * Copyright 2021 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ZenObservable from 'zen-observable'; +import { Observable, Observer, Subscription } from './observable'; + +describe('observable', () => { + it('works in conjunction with zen-observables', () => { + // Use ZenObservable as the concrete implementation, but use our types in + // all other possible places in the code + const observable: Observable = new ZenObservable( + (subscriber: Observer) => { + subscriber.next!('a'); + subscriber.error!(new Error('e')); + subscriber.complete!(); + }, + ); + + const subscription: Subscription = observable.subscribe( + _value => {}, + _error => {}, + () => {}, + ); + + subscription.unsubscribe(); + + expect(true).toBe(true); + }); +});