Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/vue3/src/components/AdvancedImage.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<img v-if="isBrowser()" ref="imageRef" />
<img v-else src="{{getSsrSrc()}}" />
<img v-else :src="getSsrSrc()" />
</template>

<script setup lang="ts">
Expand Down Expand Up @@ -37,7 +37,7 @@ const props = defineProps<ImgProps>();
const imageRef = ref(null);
let htmlLayerInstance;

const getSsrSrc = () => serverSideSrc(props.plugins, props.cldImg);
const getSsrSrc = () => serverSideSrc(props.plugins, props.cldImg, SDKAnalyticsConstants);

/**
* On mount, creates a new HTMLLayer instance and initializes with ref to img element,
Expand Down
53 changes: 53 additions & 0 deletions packages/vue3/tests/unit/analytics-ssr.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @jest-environment node
*/
import { AdvancedImage } from "../../src";
import { CloudinaryImage } from "@cloudinary/url-gen/assets/CloudinaryImage";
import { createSSRApp } from "vue";
import { renderToString } from "vue/server-renderer";
import { SDKAnalyticsConstants } from "../../src/internal/SDKAnalyticsConstants";
import { testIf } from "./utils";

const cloudinaryImage = new CloudinaryImage("sample", { cloudName: "demo" });

describe("analytics", () => {
testIf(
!(process.env.VUE3_TEST_ENV === "DIST"),
"creates an img with analytics using src",
async () => {
// Update src analytics value
SDKAnalyticsConstants.sdkSemver = "1.0.0";
SDKAnalyticsConstants.techVersion = "10.2.5";

const app = createSSRApp({
template: '<AdvancedImage :cldImg="cldImg" />',
data: () => ({
cldImg: cloudinaryImage,
}),
components: { AdvancedImage },
});
const html = await renderToString(app);
expect(html).toMatch(
'<img src="https://res.cloudinary.com/demo/image/upload/sample?_a=AL'
);
}
);

testIf(
process.env.VUE3_TEST_ENV === "DIST",
"creates an img with analytics using dist",
async () => {
const app = createSSRApp({
template: '<AdvancedImage :cldImg="cldImg" />',
data: () => ({
cldImg: cloudinaryImage,
}),
components: { AdvancedImage },
});
const html = await renderToString(app);
expect(html).toMatch(
'<img src="https://res.cloudinary.com/demo/image/upload/sample?_a=AL'
);
}
);
});
11 changes: 1 addition & 10 deletions packages/vue3/tests/unit/analytics.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import { AdvancedImage } from "../../src";
import { CloudinaryImage } from "@cloudinary/url-gen/assets/CloudinaryImage";
import { mount } from "@vue/test-utils";
import { waitTicks } from "./utils";
import { testIf, waitTicks } from "./utils";
import { SDKAnalyticsConstants } from "../../src/internal/SDKAnalyticsConstants";

const cloudinaryImage = new CloudinaryImage("sample", { cloudName: "demo" });

/**
* Run test if condition is true
* Otherwise act as passing test
* @param condition
* @param args
*/
const testIf = (condition: boolean, ...args: [string, () => void]) =>
condition ? test(...args) : {};

describe("analytics", () => {
testIf(
!(process.env.VUE3_TEST_ENV === "DIST"),
Expand Down
9 changes: 9 additions & 0 deletions packages/vue3/tests/unit/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ export const waitTicks = async (ticks: number) => {
await nextTick();
}
};

/**
* Run test if condition is true
* Otherwise act as passing test
* @param condition
* @param args
*/
export const testIf = (condition: boolean, ...args: [string, () => void]) =>
condition ? test(...args) : {};