Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
"build:babel": "babel src -d dist",
"prepublish": "npm run clean && npm run build",
"test": "jest ./tests --passWithNoTests",
"test:watch": "jest ./tests --watch",
"test:coverage": "jest ./tests --coverage",
"test:verbose": "jest ./tests --verbose",
"clean": "shx rm -rf ./coverage && shx rm -rf ./dist",
"lint:prettier": "prettier --write src/",
"lint": "eslint --ext .js . --ignore-path ./.eslintignore --resolve-plugins-relative-to .",
Expand Down
28 changes: 4 additions & 24 deletions src/settings.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
const settings = {
component: EmailTemplateBodyComponent,
// params: { footerComponent, logoTop, logoBottom, content },
params: addon1
}
component: null,
params: {}
};


return settings;


// const objectAssign = require('object-assign');

// objectAssign({foo: 0}, {bar: 1});
// //=> {foo: 0, bar: 1}

// // multiple sources
// objectAssign({foo: 0}, {bar: 1}, {baz: 2});
// //=> {foo: 0, bar: 1, baz: 2}

// // overwrites equal keys
// objectAssign({foo: 0}, {foo: 1}, {foo: 2});
// //=> {foo: 2}

// // ignores null and undefined sources
// objectAssign({foo: 0}, null, {bar: 1}, undefined);
// //=> {foo: 0, bar: 1}
export default settings;
122 changes: 122 additions & 0 deletions tests/displayFactoryTwoPointFive.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import displayFactoryTwoPointFive from '../src/displayFactoryTwoPointFive';

describe('displayFactoryTwoPointFive', () => {
let factory;

beforeEach(() => {
factory = new displayFactoryTwoPointFive();
});

describe('module exports', () => {
it('should export a class (function)', () => {
expect(typeof displayFactoryTwoPointFive).toBe('function');
});

it('should be instantiable', () => {
expect(() => new displayFactoryTwoPointFive()).not.toThrow();
});
});

describe('initialization', () => {
it('should initialize with error set to false', () => {
expect(factory.error).toBe(false);
});

it('should initialize with empty partial string', () => {
expect(factory.partial).toBe('');
});
});

describe('isError()', () => {
it('should return false when no error', () => {
expect(factory.isError()).toBe(false);
});

it('should return true when error is set', () => {
factory.error = true;
expect(factory.isError()).toBe(true);
});
});

describe('setPartial() and getPartial()', () => {
it('should set and get the partial property', () => {
factory.setPartial('<div>Content</div>');
expect(factory.getPartial()).toBe('<div>Content</div>');
});

it('should return empty string initially', () => {
expect(factory.getPartial()).toBe('');
});
});

describe('create() - basic functionality', () => {
it('should call component with params and subcomponents', () => {
const mockComponent = jest.fn().mockReturnValue('rendered');
const settings = {
component: mockComponent,
params: { id: 1, title: 'Test' },
subcomponents: []
};

const result = factory.create(settings);

expect(mockComponent).toHaveBeenCalledWith({ id: 1, title: 'Test' }, []);
expect(result).toBe('rendered');
});

it('should handle missing subcomponents', () => {
const mockComponent = jest.fn().mockReturnValue('ok');
const result = factory.create({ component: mockComponent, params: {} });
expect(result).toBe('ok');
expect(mockComponent).toHaveBeenCalledWith({}, undefined);
});
});

describe('create() - error handling', () => {
it('should handle component execution errors without throwing', () => {
const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation();
const errorComponent = jest.fn().mockImplementation(() => {
throw new Error('Component error');
});

expect(() => factory.create({ component: errorComponent, params: {} })).not.toThrow();
expect(consoleLogSpy).toHaveBeenCalled();
consoleLogSpy.mockRestore();
});

it('should return undefined when component throws', () => {
const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation();
const errorComponent = jest.fn().mockImplementation(() => {
throw new Error('fail');
});

const result = factory.create({ component: errorComponent, params: {} });
expect(result).toBeUndefined();
consoleLogSpy.mockRestore();
});
});

describe('create() - parameter handling', () => {
it('should handle complex nested parameters', () => {
const mockComponent = jest.fn().mockReturnValue('nested');
const settings = {
component: mockComponent,
params: { nested: { level1: { level2: 'value' } } }
};

const result = factory.create(settings);
expect(result).toBe('nested');
});

it('should handle array parameters', () => {
const mockComponent = jest.fn().mockReturnValue('arrays');
const settings = {
component: mockComponent,
params: { items: [1, 2, 3], names: ['a', 'b', 'c'] }
};

const result = factory.create(settings);
expect(result).toBe('arrays');
});
});
});
127 changes: 127 additions & 0 deletions tests/easyFactory.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import Factory from '../src/easyFactory';

describe('easyFactory (Factory base class)', () => {
describe('Factory.getClass()', () => {
it('should be a static method', () => {
expect(typeof Factory.getClass).toBe('function');
});

it('should throw error when called on base class', () => {
expect(() => {
Factory.getClass({ type: 'test' });
}).toThrow('This method should be implemented in the factory subclasses');
});

it('should include context in the error message', () => {
expect(() => {
Factory.getClass({ type: 'special' });
}).toThrow(/Context: \[object Object\]/);
});
});

describe('Factory.create()', () => {
it('should be a static method', () => {
expect(typeof Factory.create).toBe('function');
});

it('should throw error when called on base class (delegates to getClass)', () => {
expect(() => {
Factory.create({ type: 'test' });
}).toThrow('This method should be implemented in the factory subclasses');
});
});

describe('Subclass implementation', () => {
class AdminUser {
constructor(name) {
this.name = name;
this.role = 'admin';
}
}

class RegularUser {
constructor(name) {
this.name = name;
this.role = 'user';
}
}

class UserFactory extends Factory {
static getClass(context) {
if (context.type === 'admin') {
return AdminUser;
} else if (context.type === 'user') {
return RegularUser;
}
throw new Error('Unknown user type');
}
}

it('should create correct subclass instance for admin', () => {
const admin = UserFactory.create({ type: 'admin' }, 'John');
expect(admin).toBeInstanceOf(AdminUser);
expect(admin.role).toBe('admin');
expect(admin.name).toBe('John');
});

it('should create correct subclass instance for user', () => {
const user = UserFactory.create({ type: 'user' }, 'Jane');
expect(user).toBeInstanceOf(RegularUser);
expect(user.role).toBe('user');
expect(user.name).toBe('Jane');
});

it('should create different instances for different types', () => {
const admin = UserFactory.create({ type: 'admin' }, 'Admin User');
const user = UserFactory.create({ type: 'user' }, 'Regular User');

expect(admin).toBeInstanceOf(AdminUser);
expect(user).toBeInstanceOf(RegularUser);
});

it('should pass multiple arguments to constructor', () => {
class ExtendedUser {
constructor(name, email, age) {
this.name = name;
this.email = email;
this.age = age;
}
}

class ExtendedFactory extends Factory {
static getClass() {
return ExtendedUser;
}
}

const user = ExtendedFactory.create({}, 'Alice', 'alice@example.com', 30);
expect(user.name).toBe('Alice');
expect(user.email).toBe('alice@example.com');
expect(user.age).toBe(30);
});

it('should handle no extra arguments', () => {
class SimpleItem {
constructor() {
this.initialized = true;
}
}

class SimpleFactory extends Factory {
static getClass() {
return SimpleItem;
}
}

const result = SimpleFactory.create({});
expect(result).toBeInstanceOf(SimpleItem);
expect(result.initialized).toBe(true);
});

it('should throw for unknown context type', () => {
expect(() => {
UserFactory.create({ type: 'unknown' });
}).toThrow('Unknown user type');
});
});
});
84 changes: 84 additions & 0 deletions tests/factoryFour.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { factoryFour } from '../src/factoryFour';

describe('factoryFour', () => {
describe('function signature', () => {
it('should exist and be callable', () => {
expect(typeof factoryFour).toBe('function');
});

it('should return an object when called with valid settings', () => {
const mockComponent = jest.fn().mockReturnValue('result');
const result = factoryFour({ component: mockComponent, params: {}, subcomponents: [] });
expect(typeof result).toBe('object');
expect(result).not.toBeNull();
});
});

describe('returned object', () => {
let instance;
let mockComponent;

beforeEach(() => {
mockComponent = jest.fn().mockReturnValue('rendered');
instance = factoryFour({
component: mockComponent,
params: { name: 'Test', value: 42 },
subcomponents: []
});
});

it('should expose params on returned object', () => {
expect(instance.params).toEqual({ name: 'Test', value: 42 });
});

it('should expose changeParams method', () => {
expect(typeof instance.changeParams).toBe('function');
});

it('should expose start method', () => {
expect(typeof instance.start).toBe('function');
});

it('should expose display method', () => {
expect(typeof instance.display).toBe('function');
});

it('should merge params via changeParams()', () => {
instance.changeParams({ extra: 'value' });
expect(instance.params).toEqual({ name: 'Test', value: 42, extra: 'value' });
});

it('should overwrite existing params via changeParams()', () => {
instance.changeParams({ name: 'Updated' });
expect(instance.params.name).toBe('Updated');
});

it('should call the component function on display()', () => {
instance.display();
expect(mockComponent).toHaveBeenCalledWith(
{ name: 'Test', value: 42 },
[]
);
});

it('should return the component result from display()', () => {
const result = instance.display();
expect(result).toBe('rendered');
});
});

describe('parameter handling', () => {
it('should handle empty params object', () => {
const mockComponent = jest.fn().mockReturnValue('ok');
expect(() => factoryFour({ component: mockComponent, params: {} })).not.toThrow();
});

it('should return consistent results for same input', () => {
const mockComponent = jest.fn().mockReturnValue('same');
const params = { id: 1 };
const result1 = factoryFour({ component: mockComponent, params });
const result2 = factoryFour({ component: mockComponent, params });
expect(result1.params).toEqual(result2.params);
});
});
});
Loading