usecase_ts
Version:
Uma implementação do padrão Result para TypeScript
98 lines (97 loc) • 4.38 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const context_1 = require("./context");
describe('Context Class', () => {
describe('constructor', () => {
it('should store input and output parameters', () => {
const input = { id: '123', name: 'Test' };
const output = { result: 'Success', count: 5 };
const context = new context_1.Context(input, output);
expect(context._inputParams).toBe(input);
expect(context._outputParams).toBe(output);
});
it('should handle null or undefined input', () => {
const output = { result: 'Success' };
const context = new context_1.Context(null, output);
expect(context._inputParams).toEqual({});
expect(context._outputParams).toBe(output);
});
it('should handle null or undefined output', () => {
const input = { id: '123' };
const context = new context_1.Context(input, null);
expect(context._inputParams).toBe(input);
expect(context._outputParams).toEqual({});
});
it('should create properties from input and output', () => {
const input = { id: '123', name: 'Test' };
const output = { result: 'Success', count: 5 };
const context = new context_1.Context(input, output);
expect(context.id).toBe('123');
expect(context.name).toBe('Test');
expect(context.result).toBe('Success');
expect(context.count).toBe(5);
});
it('should not override existing properties', () => {
// Modificado: Usando getter em vez de propriedade readonly
class TestContext extends context_1.Context {
get id() {
return 'fixed';
}
}
const input = { id: '123', name: 'Test' };
const output = { result: 'Success' };
const context = new TestContext(input, output);
// O id property não deve ser sobrescrito
expect(context.id).toBe('fixed');
expect(context.name).toBe('Test');
expect(context.result).toBe('Success');
});
it('should make properties non-writable', () => {
const input = { id: '123' };
const output = { result: 'Success' };
const context = new context_1.Context(input, output);
// Attempt to modify a property
expect(() => {
context.id = 'new-id';
}).toThrow();
});
});
describe('getInput method', () => {
it('should return the input parameters', () => {
const input = { id: '123', name: 'Test' };
const output = { result: 'Success' };
const context = new context_1.Context(input, output);
expect(context.getInput()).toBe(input);
});
});
describe('getOutput method', () => {
it('should return the output parameters', () => {
const input = { id: '123' };
const output = { result: 'Success', count: 5 };
const context = new context_1.Context(input, output);
expect(context.getOutput()).toBe(output);
});
});
describe('edge cases', () => {
it('should handle empty objects', () => {
const context = new context_1.Context({}, {});
expect(context._inputParams).toEqual({});
expect(context._outputParams).toEqual({});
});
it('should handle objects with same property names', () => {
const input = { id: '123', shared: 'input' };
const output = { result: 'Success', shared: 'output' };
const context = new context_1.Context(input, output);
// Output properties should override input properties with the same name
expect(context.shared).toBe('output');
});
it('should handle non-primitive property values', () => {
const nestedObject = { nested: 'value' };
const input = { obj: nestedObject };
const output = { arr: [1, 2, 3] };
const context = new context_1.Context(input, output);
expect(context.obj).toBe(nestedObject);
expect(context.arr).toEqual([1, 2, 3]);
});
});
});