-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create first service (browser)
Implement a simple infrastructure with a single service and method
- Loading branch information
Showing
11 changed files
with
132 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import ContainerService from '../container-service' | ||
import { IContainerService } from '../services' | ||
|
||
const createMockService = (applies: boolean, start: () => void): IContainerService => ({ | ||
applies: () => applies, | ||
start, | ||
}) | ||
|
||
describe('Container Service', () => { | ||
const service1Applies = false | ||
const service1Start = jest.fn() | ||
const mockService1 = createMockService(service1Applies, service1Start) | ||
|
||
const service2Applies = true | ||
const service2Start = jest.fn() | ||
const mockService2 = createMockService(service2Applies, service2Start) | ||
|
||
beforeEach(() => { | ||
service1Start.mockReset() | ||
service2Start.mockReset() | ||
}) | ||
|
||
it('selects the first strategy that applies (1)', () => { | ||
const containerService = new ContainerService([mockService1, mockService2]) | ||
containerService.start() | ||
|
||
expect(service1Start).toHaveBeenCalledTimes(0) | ||
expect(service2Start).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('selects the first strategy that applies (2)', () => { | ||
const containerService = new ContainerService([mockService2, mockService1]) | ||
containerService.start() | ||
|
||
expect(service1Start).toHaveBeenCalledTimes(0) | ||
expect(service2Start).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('start calls start on the service', () => { | ||
const containerService = new ContainerService([mockService2]) | ||
expect(service2Start).toHaveBeenCalledTimes(0) | ||
|
||
containerService.start() | ||
expect(service2Start).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import services from '../services' | ||
import ContainerService from '../container-service' | ||
|
||
import containerService from '../index' | ||
|
||
describe('containerService', () => { | ||
it('has a double function', () => { | ||
expect(containerService.double(5)).toBe(10) | ||
it('is constructed with the services', () => { | ||
expect(containerService).toEqual(new ContainerService(services)) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { IContainerService } from './services' | ||
|
||
class ContainerService { | ||
private service: IContainerService | ||
|
||
constructor(services: IContainerService[]) { | ||
this.service = services.find(s => s.applies()) | ||
} | ||
|
||
public start() { | ||
this.service.start() | ||
} | ||
} | ||
|
||
export default ContainerService |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
const containerService = { | ||
double: (input: number) => input * 2, | ||
} | ||
import services from './services' | ||
import ContainerService from './container-service' | ||
|
||
export default containerService | ||
export default new ContainerService(services) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import BrowserService from '../browser-service' | ||
|
||
const browserService = new BrowserService() | ||
|
||
describe('BrowserService', () => { | ||
it('applies returns true', () => { | ||
expect(browserService.applies()).toBe(true) | ||
}) | ||
|
||
it('start does not throw an error', () => { | ||
browserService.start() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const mockBrowserService = class {} | ||
jest.mock('../browser-service', () => ({ | ||
default: mockBrowserService, | ||
})) | ||
|
||
import services from '../services' | ||
|
||
describe('services', () => { | ||
it('combines all the services in the correct order', () => { | ||
expect(services).toEqual([new mockBrowserService()]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import IContainerService from './container-service' | ||
|
||
class BrowserService implements IContainerService { | ||
public applies() { | ||
return true | ||
} | ||
|
||
public start() { | ||
// No-op | ||
} | ||
} | ||
|
||
export default BrowserService |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
interface IContainerService { | ||
applies(): boolean | ||
start(): void | ||
} | ||
|
||
export default IContainerService |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import IContainerService from './container-service' | ||
|
||
import services from './services' | ||
|
||
export default services | ||
export { IContainerService } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import BrowserService from './browser-service' | ||
|
||
const services = [ | ||
new BrowserService(), | ||
] | ||
|
||
export default services |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters