Skip to content

Commit ac9c5cd

Browse files
committed
feat(light): new 'Disco' use case
1 parent 40314d7 commit ac9c5cd

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import stc from "string-to-color";
2+
import { Reward, Light } from "../../entities";
3+
import { LifxLightService, LightService } from "../../services";
4+
import { RewardActionUseCase } from "./RewardActionUseCase";
5+
6+
type PerformOptions = {
7+
reward: Reward;
8+
lights: Light[];
9+
};
10+
11+
export class Disco implements RewardActionUseCase {
12+
public constructor(
13+
private lightService: LightService = new LifxLightService()
14+
) {}
15+
16+
public async perform({ reward, lights }: PerformOptions): Promise<void> {
17+
const color = stc(reward.message);
18+
const initialColor = stc(reward.date);
19+
20+
await Promise.all(
21+
lights.map((light) =>
22+
this.lightService.disco(light, {
23+
color,
24+
initialColor,
25+
cycles: 5,
26+
period: 20,
27+
})
28+
)
29+
);
30+
}
31+
}
32+
33+
export default Disco;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import stc from "string-to-color";
2+
import { FakeLightService } from "../../../src";
3+
import { Disco } from "../../../src/useCases/rewards/Disco";
4+
import { LightBuilder } from "../../builders/LightBuilder";
5+
import { RewardBuilder } from "../../builders/RewardBuilder";
6+
7+
jest.mock("string-to-color", () => jest.fn(() => "#fefefe"));
8+
9+
describe("Disco", () => {
10+
describe("#perform", () => {
11+
it("uses the reward message to generate the color for the disco mode", async () => {
12+
const lightService = new FakeLightService();
13+
const subject = new Disco(lightService);
14+
const reward = RewardBuilder.build();
15+
const lights = LightBuilder.buildList(1);
16+
17+
await subject.perform({ reward, lights });
18+
19+
expect(stc).toHaveBeenCalledWith(reward.message);
20+
});
21+
22+
it("uses the reward date to generate the initialColor for the disco mode", async () => {
23+
const lightService = new FakeLightService();
24+
const subject = new Disco(lightService);
25+
const reward = RewardBuilder.build();
26+
const lights = LightBuilder.buildList(1);
27+
28+
await subject.perform({ reward, lights });
29+
30+
expect(stc).toHaveBeenCalledWith(reward.date);
31+
});
32+
33+
it("uses light service to start the disco mode", async () => {
34+
const lightService = new FakeLightService();
35+
const subject = new Disco(lightService);
36+
const reward = RewardBuilder.build();
37+
const lights = LightBuilder.buildList(1);
38+
39+
await subject.perform({ reward, lights });
40+
41+
expect(lightService.disco).toHaveBeenCalledWith(lights[0], {
42+
color: "#fefefe",
43+
initialColor: "#fefefe",
44+
cycles: 5,
45+
period: 20,
46+
});
47+
});
48+
});
49+
});

0 commit comments

Comments
 (0)