-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct-video.spec.tsx
127 lines (100 loc) · 4.2 KB
/
product-video.spec.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* @jest-environment jsdom
*/
import type {
ProductTag,
} from "@medusajs/medusa";
import type { ProductDetailsWidgetProps } from "@medusajs/admin"
import { render, screen, cleanup, waitFor, fireEvent } from "@testing-library/react";
import userEvent from '@testing-library/user-event';
import '@testing-library/jest-dom'
import { mock } from 'jest-mock-extended';
import nock from 'nock';
import { MedusaProvider as Provider } from "medusa-react"
import { QueryClient } from "@tanstack/react-query"
export const queryClient = new QueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
staleTime: 90000,
retry: 1,
},
},
})
import ProductVideo from '../../widgets/product-video';
describe('product video admin widget', () => {
afterEach(() => {
nock.cleanAll();
queryClient.clear();
});
it('renders preview if product has video', async () => {
nock("http://localhost/test")
.defaultReplyHeaders({
"access-control-allow-origin": "*",
})
.get("/admin/product-video/1234")
.reply(200, {
"id": "product-video_01J4YJCAZ5J20K1ZSPDFAV675A",
"created_at": "2024-08-10T16:36:37.477Z",
"updated_at": "2024-08-10T16:36:37.477Z",
"video_id": "abc",
"product_id": "1234",
"embedUrl": "https://www.youtube.com/embed/blah"
});
const mockProductDetails = mock<ProductDetailsWidgetProps>()
mockProductDetails.product.id = "1234"
render(
<Provider baseUrl="test" queryClientProviderProps={{ client: queryClient }}>
<ProductVideo {...mockProductDetails} />
</Provider>
);
await (waitFor(() => screen.getByText('Product Video'), { timeout: 3000 }));
const header = screen.getByTestId("product-video-header");
const videoIframe = screen.getByTestId("product-video-iframe");
expect(videoIframe).toHaveAttribute('src', 'https://www.youtube.com/embed/abc')
expect(header).toHaveTextContent("Product Video")
});
it('supports adding a new video', async () => {
const mockVideoId = "def1";
nock("http://localhost/test")
.defaultReplyHeaders({
"access-control-allow-origin": "*",
})
.get("/admin/product-video/1234")
.reply(200, null)
.post("/admin/product-video/1234")
.reply(200, {
"id": "product-video_01J4YJCAZ5J20K1ZSPDFAV675A",
"created_at": "2024-08-10T16:36:37.477Z",
"updated_at": "2024-08-10T16:36:37.477Z",
"video_id": mockVideoId,
"product_id": "1234",
"embedUrl": "https://www.youtube.com/embed/blah"
})
.get("/admin/product-video/1234")
.reply(200, {
"id": "product-video_01J4YJCAZ5J20K1ZSPDFAV675A",
"created_at": "2024-08-10T16:36:37.477Z",
"updated_at": "2024-08-10T16:36:37.477Z",
"video_id": mockVideoId,
"product_id": "1234",
"embedUrl": "https://www.youtube.com/embed/blah"
})
const mockProductDetails = mock<ProductDetailsWidgetProps>()
mockProductDetails.product.id = "1234"
render(
<Provider baseUrl="test" queryClientProviderProps={{ client: queryClient }}>
<ProductVideo {...mockProductDetails} />
</Provider>
);
await (waitFor(() => screen.getByText('Product Video'), { timeout: 3000 }));
const input = screen.getByPlaceholderText('Enter video ID');
// Simulate typing into the input
fireEvent.change(input, { target: { value: mockVideoId } });
const button = screen.getByText('Add Video');
await userEvent.click(button);
await (waitFor(() => screen.getByTestId("product-video-iframe"), { timeout: 3000 }));
const videoIframe = screen.getByTestId("product-video-iframe");
expect(videoIframe).toHaveAttribute('src', `https://www.youtube.com/embed/${mockVideoId}`)
});
});