mirror of
https://github.com/bitwarden/browser
synced 2025-12-17 16:53:34 +00:00
[PM-7289] implement generator libraries (#9549)
This is a copy of the files. The source in `@bitwarden/common` will be deleted once all of the applications have been ported to the library.
This commit is contained in:
86
libs/common/src/tools/rx.spec.ts
Normal file
86
libs/common/src/tools/rx.spec.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* include structuredClone in test environment.
|
||||
* @jest-environment ../../../../shared/test.environment.ts
|
||||
*/
|
||||
import { of, firstValueFrom } from "rxjs";
|
||||
|
||||
import { awaitAsync, trackEmissions } from "../../spec";
|
||||
|
||||
import { distinctIfShallowMatch, reduceCollection } from "./rx";
|
||||
|
||||
describe("reduceCollection", () => {
|
||||
it.each([[null], [undefined], [[]]])(
|
||||
"should return the default value when the collection is %p",
|
||||
async (value: number[]) => {
|
||||
const reduce = (acc: number, value: number) => acc + value;
|
||||
const source$ = of(value);
|
||||
|
||||
const result$ = source$.pipe(reduceCollection(reduce, 100));
|
||||
const result = await firstValueFrom(result$);
|
||||
|
||||
expect(result).toEqual(100);
|
||||
},
|
||||
);
|
||||
|
||||
it("should reduce the collection to a single value", async () => {
|
||||
const reduce = (acc: number, value: number) => acc + value;
|
||||
const source$ = of([1, 2, 3]);
|
||||
|
||||
const result$ = source$.pipe(reduceCollection(reduce, 0));
|
||||
const result = await firstValueFrom(result$);
|
||||
|
||||
expect(result).toEqual(6);
|
||||
});
|
||||
});
|
||||
|
||||
describe("distinctIfShallowMatch", () => {
|
||||
it("emits a single value", async () => {
|
||||
const source$ = of({ foo: true });
|
||||
const pipe$ = source$.pipe(distinctIfShallowMatch());
|
||||
|
||||
const result = trackEmissions(pipe$);
|
||||
await awaitAsync();
|
||||
|
||||
expect(result).toEqual([{ foo: true }]);
|
||||
});
|
||||
|
||||
it("emits different values", async () => {
|
||||
const source$ = of({ foo: true }, { foo: false });
|
||||
const pipe$ = source$.pipe(distinctIfShallowMatch());
|
||||
|
||||
const result = trackEmissions(pipe$);
|
||||
await awaitAsync();
|
||||
|
||||
expect(result).toEqual([{ foo: true }, { foo: false }]);
|
||||
});
|
||||
|
||||
it("emits new keys", async () => {
|
||||
const source$ = of({ foo: true }, { foo: true, bar: true });
|
||||
const pipe$ = source$.pipe(distinctIfShallowMatch());
|
||||
|
||||
const result = trackEmissions(pipe$);
|
||||
await awaitAsync();
|
||||
|
||||
expect(result).toEqual([{ foo: true }, { foo: true, bar: true }]);
|
||||
});
|
||||
|
||||
it("suppresses identical values", async () => {
|
||||
const source$ = of({ foo: true }, { foo: true });
|
||||
const pipe$ = source$.pipe(distinctIfShallowMatch());
|
||||
|
||||
const result = trackEmissions(pipe$);
|
||||
await awaitAsync();
|
||||
|
||||
expect(result).toEqual([{ foo: true }]);
|
||||
});
|
||||
|
||||
it("suppresses removed keys", async () => {
|
||||
const source$ = of({ foo: true, bar: true }, { foo: true });
|
||||
const pipe$ = source$.pipe(distinctIfShallowMatch());
|
||||
|
||||
const result = trackEmissions(pipe$);
|
||||
await awaitAsync();
|
||||
|
||||
expect(result).toEqual([{ foo: true, bar: true }]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user