test: make image-cache tests async (#8232)

This commit is contained in:
Darin Dimitrov
2020-01-08 15:04:29 +02:00
committed by GitHub
parent a8cce271b5
commit 3c340b42dc

View File

@@ -1,14 +1,11 @@
import * as imageCacheModule from "@nativescript/core/ui/image-cache";
import { ImageSource } from "@nativescript/core/image-source";
import * as types from "@nativescript/core/utils/types";
import { isAndroid, device } from "@nativescript/core/platform";
import lazy from "@nativescript/core/utils/lazy";
import * as TKUnit from "../../tk-unit";
const sdkVersion = lazy(() => parseInt(device.sdkVersion));
export const test_ImageCache_ValidUrl = function () {
export const test_ImageCache_ValidUrl = function (done: (err: Error, res?: string) => void) {
// see https://github.com/NativeScript/NativeScript/issues/6643
if (isAndroid && sdkVersion() < 20) {
return;
@@ -17,8 +14,6 @@ export const test_ImageCache_ValidUrl = function () {
const cache = new imageCacheModule.Cache();
cache.maxRequests = 5;
let validKey: string;
let imgSource: ImageSource;
const url = "https://github.com/NativeScript.png";
// Try to read the image from the cache
@@ -26,6 +21,7 @@ export const test_ImageCache_ValidUrl = function () {
if (image) {
// If present -- use it.
imgSource = new ImageSource(image);
done(new Error("The image was found in the cache"));
}
else {
// If not present -- request its download.
@@ -35,24 +31,18 @@ export const test_ImageCache_ValidUrl = function () {
completed: (image: any, key: string) => {
if (url === key) {
imgSource = new ImageSource(image);
validKey = key;
console.log("Valid url: ", key);
done(null);
}
}
});
}
TKUnit.waitUntilReady(() => types.isDefined(validKey), 8);
TKUnit.assertEqual(validKey, url, "Key should equal the provided url");
};
export const test_ImageCache_NothingAtProvidedUrl = function () {
export const test_ImageCache_NothingAtProvidedUrl = function (done: (err: Error, res?: string) => void) {
const cache = new imageCacheModule.Cache();
cache.maxRequests = 5;
let errorCaught = false;
let errorMessage: string;
let imgSource: ImageSource;
const url = "https://github.com/NativeScript-NoImage.png";
// Try to read the image from the cache
@@ -60,6 +50,7 @@ export const test_ImageCache_NothingAtProvidedUrl = function () {
if (image) {
// If present -- use it.
imgSource = new ImageSource(image);
done(new Error("The image was found in the cache"));
}
else {
// If not present -- request its download.
@@ -70,15 +61,12 @@ export const test_ImageCache_NothingAtProvidedUrl = function () {
if (url === key) {
imgSource = new ImageSource(image);
}
done(new Error("The completed callback was not expected to be called"));
},
error: (key: string) => {
console.log("No image for key: ", key);
errorMessage = `No image for key: ${key}`;
errorCaught = true;
done(null);
}
});
}
TKUnit.waitUntilReady(() => errorCaught);
TKUnit.assertEqual(`No image for key: ${url}`, errorMessage);
};