parent
05ef6a8ca3
commit
e76ca380f7
16 changed files with 218 additions and 96 deletions
@ -1,19 +0,0 @@ |
||||
import * as messages from '../../shared/messages'; |
||||
import * as actions from './index'; |
||||
|
||||
const enable = (): Promise<actions.AddonAction> => setEnabled(true); |
||||
|
||||
const disable = (): Promise<actions.AddonAction> => setEnabled(false); |
||||
|
||||
const setEnabled = async(enabled: boolean): Promise<actions.AddonAction> => { |
||||
await browser.runtime.sendMessage({ |
||||
type: messages.ADDON_ENABLED_RESPONSE, |
||||
enabled, |
||||
}); |
||||
return { |
||||
type: actions.ADDON_SET_ENABLED, |
||||
enabled, |
||||
}; |
||||
}; |
||||
|
||||
export { enable, disable, setEnabled }; |
@ -0,0 +1,16 @@ |
||||
import * as messages from '../../shared/messages'; |
||||
|
||||
export default interface AddonIndicatorClient { |
||||
setEnabled(enabled: boolean): Promise<void>; |
||||
|
||||
// eslint-disable-next-line semi
|
||||
} |
||||
|
||||
export class AddonIndicatorClientImpl implements AddonIndicatorClient { |
||||
setEnabled(enabled: boolean): Promise<void> { |
||||
return browser.runtime.sendMessage({ |
||||
type: messages.ADDON_ENABLED_RESPONSE, |
||||
enabled, |
||||
}); |
||||
} |
||||
} |
@ -1,22 +0,0 @@ |
||||
import * as actions from '../actions'; |
||||
|
||||
export interface State { |
||||
enabled: boolean; |
||||
} |
||||
|
||||
const defaultState: State = { |
||||
enabled: true, |
||||
}; |
||||
|
||||
export default function reducer( |
||||
state: State = defaultState, |
||||
action: actions.AddonAction, |
||||
): State { |
||||
switch (action.type) { |
||||
case actions.ADDON_SET_ENABLED: |
||||
return { ...state, |
||||
enabled: action.enabled, }; |
||||
default: |
||||
return state; |
||||
} |
||||
} |
@ -0,0 +1,19 @@ |
||||
let enabled: boolean = false; |
||||
|
||||
export default interface AddonEnabledRepository { |
||||
set(on: boolean): void; |
||||
|
||||
get(): boolean; |
||||
|
||||
// eslint-disable-next-line semi
|
||||
} |
||||
|
||||
export class AddonEnabledRepositoryImpl implements AddonEnabledRepository { |
||||
set(on: boolean): void { |
||||
enabled = on; |
||||
} |
||||
|
||||
get(): boolean { |
||||
return enabled; |
||||
} |
||||
} |
@ -0,0 +1,40 @@ |
||||
import AddonIndicatorClient, { AddonIndicatorClientImpl } |
||||
from '../client/AddonIndicatorClient'; |
||||
import AddonEnabledRepository, { AddonEnabledRepositoryImpl } |
||||
from '../repositories/AddonEnabledRepository'; |
||||
|
||||
export default class AddonEnabledUseCase { |
||||
private indicator: AddonIndicatorClient; |
||||
|
||||
private repository: AddonEnabledRepository; |
||||
|
||||
constructor({ |
||||
indicator = new AddonIndicatorClientImpl(), |
||||
repository = new AddonEnabledRepositoryImpl(), |
||||
} = {}) { |
||||
this.indicator = indicator; |
||||
this.repository = repository; |
||||
} |
||||
|
||||
async enable(): Promise<void> { |
||||
await this.setEnabled(true); |
||||
} |
||||
|
||||
async disable(): Promise<void> { |
||||
await this.setEnabled(false); |
||||
} |
||||
|
||||
async toggle(): Promise<void> { |
||||
let current = this.repository.get(); |
||||
await this.setEnabled(!current); |
||||
} |
||||
|
||||
getEnabled(): boolean { |
||||
return this.repository.get(); |
||||
} |
||||
|
||||
private async setEnabled(on: boolean): Promise<void> { |
||||
this.repository.set(on); |
||||
await this.indicator.setEnabled(on); |
||||
} |
||||
} |
@ -1,17 +0,0 @@ |
||||
import * as actions from 'content/actions'; |
||||
import addonReducer from 'content/reducers/addon'; |
||||
|
||||
describe("addon reducer", () => { |
||||
it('return the initial state', () => { |
||||
let state = addonReducer(undefined, {}); |
||||
expect(state).to.have.property('enabled', true); |
||||
}); |
||||
|
||||
it('return next state for ADDON_SET_ENABLED', () => { |
||||
let action = { type: actions.ADDON_SET_ENABLED, enabled: true }; |
||||
let prev = { enabled: false }; |
||||
let state = addonReducer(prev, action); |
||||
|
||||
expect(state.enabled).is.equal(true); |
||||
}); |
||||
}); |
@ -0,0 +1,15 @@ |
||||
import { AddonEnabledRepositoryImpl } from '../../../src/content/repositories/AddonEnabledRepository'; |
||||
import { expect } from 'chai'; |
||||
|
||||
describe('AddonEnabledRepositoryImpl', () => { |
||||
it('updates and gets current value', () => { |
||||
let sut = new AddonEnabledRepositoryImpl(); |
||||
|
||||
sut.set(true); |
||||
expect(sut.get()).to.be.true; |
||||
|
||||
sut.set(false); |
||||
expect(sut.get()).to.be.false; |
||||
}); |
||||
}); |
||||
|
@ -0,0 +1,90 @@ |
||||
import AddonEnabledRepository from '../../../src/content/repositories/AddonEnabledRepository'; |
||||
import AddonEnabledUseCase from '../../../src/content/usecases/AddonEnabledUseCase'; |
||||
import AddonIndicatorClient from '../../../src/content/client/AddonIndicatorClient'; |
||||
import { expect } from 'chai'; |
||||
|
||||
class MockAddonEnabledRepository implements AddonEnabledRepository { |
||||
private enabled: boolean; |
||||
|
||||
constructor(init: boolean) { |
||||
this.enabled = init; |
||||
} |
||||
|
||||
set(on: boolean): void { |
||||
this.enabled = on; |
||||
} |
||||
|
||||
get(): boolean { |
||||
return this.enabled; |
||||
} |
||||
} |
||||
|
||||
class MockAddonIndicatorClient implements AddonIndicatorClient { |
||||
public enabled: boolean; |
||||
|
||||
constructor(init: boolean) { |
||||
this.enabled = init; |
||||
} |
||||
|
||||
async setEnabled(enabled: boolean): Promise<void> { |
||||
this.enabled = enabled; |
||||
return |
||||
} |
||||
} |
||||
|
||||
describe('AddonEnabledUseCase', () => { |
||||
let repository: MockAddonEnabledRepository; |
||||
let indicator: MockAddonIndicatorClient; |
||||
let sut: AddonEnabledUseCase; |
||||
|
||||
beforeEach(() => { |
||||
repository = new MockAddonEnabledRepository(true); |
||||
indicator = new MockAddonIndicatorClient(false); |
||||
sut = new AddonEnabledUseCase({ repository, indicator }); |
||||
}); |
||||
|
||||
describe('#enable', () => { |
||||
it('store and indicate as enabled', async() => { |
||||
await sut.enable(); |
||||
|
||||
expect(repository.get()).to.be.true; |
||||
expect(indicator.enabled).to.be.true; |
||||
}); |
||||
}); |
||||
|
||||
describe('#disable', async() => { |
||||
it('store and indicate as disabled', async() => { |
||||
await sut.disable(); |
||||
|
||||
expect(repository.get()).to.be.false; |
||||
expect(indicator.enabled).to.be.false; |
||||
}); |
||||
}); |
||||
|
||||
describe('#toggle', () => { |
||||
it('toggled enabled and disabled', async() => { |
||||
repository.set(true); |
||||
await sut.toggle(); |
||||
|
||||
expect(repository.get()).to.be.false; |
||||
expect(indicator.enabled).to.be.false; |
||||
|
||||
repository.set(false); |
||||
|
||||
await sut.toggle(); |
||||
|
||||
expect(repository.get()).to.be.true; |
||||
expect(indicator.enabled).to.be.true; |
||||
}); |
||||
}); |
||||
|
||||
describe('#getEnabled', () => { |
||||
it('returns current addon enabled', () => { |
||||
repository.set(true); |
||||
expect(sut.getEnabled()).to.be.true; |
||||
|
||||
repository.set(false); |
||||
expect(sut.getEnabled()).to.be.false; |
||||
}); |
||||
}); |
||||
}); |
Reference in new issue