-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split image loading into its own class
- Loading branch information
1 parent
896d663
commit 7f406ad
Showing
9 changed files
with
121 additions
and
34 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
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,28 @@ | ||
import { Test, TestSuite } from "xunit.ts"; | ||
import AppViewModel from "../App/AppViewModel"; | ||
import Mockito from "ts-mockito"; | ||
import ImageLoader from "../App/ImageLoader"; | ||
|
||
export default class AppViewModelTests extends TestSuite | ||
{ | ||
@Test() | ||
async canSetBackgroundFromImageLoaderData() { | ||
//arrange | ||
const file = Mockito.mock<File>(); | ||
|
||
const loader = Mockito.mock<ImageLoader>(); | ||
Mockito.when(loader.loadImage(file)).thenResolve("data:image/png;base64,abc123"); | ||
const vm = new AppViewModel(); | ||
vm.image_loader = Mockito.instance(loader); | ||
|
||
const files = Mockito.mock<FileList>(); | ||
Mockito.when(files.length).thenReturn(1); | ||
Mockito.when(files.item(0)).thenReturn(file); | ||
|
||
//act | ||
await vm.setBackground(Mockito.instance(files)); | ||
|
||
//assert | ||
this.assert.equal("data:image/png;base64,abc123", vm.background); | ||
} | ||
} |
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,20 @@ | ||
import {Test, TestSuite} from "xunit.ts"; | ||
import ImageLoader from "../App/ImageLoader"; | ||
|
||
export default class ImageLoaderTests extends TestSuite { | ||
@Test() | ||
async canLoadImage() { | ||
//arrange | ||
const reader = new FileReader(); | ||
const loader = new ImageLoader(reader); | ||
|
||
const blob = ["abc123"]; | ||
const file = new File(blob, "test.txt", { type: "text/plain" }); | ||
|
||
//act | ||
const base64 = await loader.loadImage(file); | ||
|
||
//assert | ||
this.assert.equal("data:text/plain;base64,YWJjMTIz", base64); | ||
} | ||
} |
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
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
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
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,16 @@ | ||
export default class ImageLoader { | ||
|
||
private readonly file_reader: FileReader; | ||
|
||
constructor(file_reader: FileReader) { | ||
this.file_reader = file_reader; | ||
} | ||
|
||
async loadImage(file: Blob) { | ||
return new Promise<string>((resolve, reject) => { | ||
this.file_reader.onerror = () => reject(new DOMException("Could not read file")); | ||
this.file_reader.onload = () => resolve(this.file_reader.result as string); | ||
this.file_reader.readAsDataURL(file); | ||
}); | ||
} | ||
} |
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,15 +1,15 @@ | ||
import Vue from "vue"; | ||
import app from "./app.vue"; | ||
import Factory from "./Factory"; | ||
import Signal from "./Signal"; | ||
|
||
Vue.config.devtools = true; | ||
|
||
export default new Vue({ | ||
el: "app", | ||
render: (r: Vue.CreateElement) => r(app), | ||
provide: { | ||
signal_service: (signals: Signal[]) => Factory.signalService(signals), | ||
renderer: (canvas: HTMLCanvasElement) => Factory.renderer(canvas) | ||
signal_service: Factory.signalService, | ||
renderer: Factory.renderer, | ||
image_loader: Factory.imageLoader | ||
} | ||
}); |
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