Skip to content

Commit

Permalink
tests(context): Add test coverage to use and useX
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Mar 28, 2022
1 parent a087969 commit 3d5b4e4
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Context Context.run Should clear context after callback run 1`] = `
exports[`Context context.run Should clear context after callback run 1`] = `
Object {
"a": 1,
}
`;

exports[`Context Context.run Should clear context after callback run 2`] = `
exports[`Context context.run Should clear context after callback run 2`] = `
Object {
"a": 1,
"b": 2,
Expand Down
87 changes: 86 additions & 1 deletion packages/context/src/__tests__/context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('Context', () => {
});
});

describe('Context.run', () => {
describe('context.run', () => {
it('Should create a new context instance', () => {
const top = ctx.use();

Expand Down Expand Up @@ -186,6 +186,91 @@ describe('Context', () => {
});
});

describe('context.use', () => {
describe('When in an active context', () => {
it('Should return a cloned ctxRef object', () => {
const ctxRef = { a: 1, b: 2 };

ctx.run(ctxRef, () => {
expect(ctx.use()).toEqual(ctxRef);
});
});

it('Should return a frozen context object', () => {
const ctxRef = { a: 1, b: 2 };

ctx.run(ctxRef, () => {
expect(Object.isFrozen(ctx.use())).toBe(true);
});
});

describe('When before running the context', () => {
it('Should return undefined', () => {
expect(ctx.use()).toBeUndefined();
});
});

describe('When after closing the context', () => {
it('Should return undefined', () => {
ctx.run({}, () => {});
expect(ctx.use()).toBeNull();
});
});
});
});

describe('context.useX', () => {
describe('When in an active context', () => {
it('Should return a cloned ctxRef object', () => {
const ctxRef = { a: 1, b: 2 };

ctx.run(ctxRef, () => {
expect(ctx.useX()).toEqual(ctxRef);
});
});

it('Should return a frozen context object', () => {
const ctxRef = { a: 1, b: 2 };

ctx.run(ctxRef, () => {
expect(Object.isFrozen(ctx.useX())).toBe(true);
});
});

describe('When before running the context', () => {
it('Should throw error', () => {
expect(() => ctx.useX()).toThrow(
'Context was used after it was closed'
);
});

it('Should allow a custom context message', () => {
expect(() => ctx.useX('Custom Failure Message')).toThrow(
'Custom Failure Message'
);
});
});

describe('When after closing the context', () => {
beforeEach(() => {
ctx.run({}, () => {});
});

it('Should return undefined', () => {
expect(() => ctx.useX()).toThrow(
'Context was used after it was closed'
);
});

it('Should allow a custom context message', () => {
expect(() => ctx.useX('Custom Failure Message')).toThrow(
'Custom Failure Message'
);
});
});
});
});

describe('init argument', () => {
it('Should run init function on every context.run', () => {
const init = jest.fn();
Expand Down

0 comments on commit 3d5b4e4

Please sign in to comment.