Skip to content

Commit 1765d29

Browse files
committed
ex-1: throw error if employee type is invalid at createEmployee
1 parent f2cbc5e commit 1765d29

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

src/example-01/emplyees/factory/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const createEmployee = (name, type) => {
1111
return new Salesman(name);
1212
case 'manager':
1313
return new Manager(name);
14+
default:
15+
throw new Error(`Employee cannot be of type ${type}`);
1416
}
15-
return new Employee(name, type);
1617
};

src/example-01/emplyees/factory/index.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ describe('createEmployee', () => {
2222
expect(employee.type).toBe('manager');
2323
expect(employee instanceof Manager).toBe(true);
2424
});
25+
26+
it('should throw an error for an unknown type', () => {
27+
expect(() => createEmployee('Kaio', 'invalid')).toThrow('Employee cannot be of type invalid');
28+
});
2529
});

0 commit comments

Comments
 (0)