ImageSelect.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { mount, createLocalVue } from '@vue/test-utils'
  2. import Vuex from 'vuex'
  3. import ImageSelect from '../ImageSelect'
  4. import { IMAGES_TYPE_MAP } from '@/constants/compute'
  5. import { Manager } from '@/utils/manager'
  6. import i18n from '@/locales'
  7. const idcStandardImageList = [
  8. {
  9. name: 'test1',
  10. id: 1,
  11. disk_format: 'qcow2',
  12. properties: {
  13. arch: 'x86_64',
  14. distro: 'Windows Server 2012 R2 Datacenter',
  15. os_type: 'Windows',
  16. partition_type: 'mbr',
  17. version: '6.3',
  18. },
  19. },
  20. {
  21. name: 'test2',
  22. id: 2,
  23. disk_format: 'qcow2',
  24. properties: {
  25. net_driver: '',
  26. os_distribution: 'CentOS',
  27. os_type: 'Linux',
  28. partition_type: 'mbr',
  29. uefi_support: 'true',
  30. version: '8.0.1905',
  31. },
  32. },
  33. {
  34. name: 'test31',
  35. id: 3,
  36. disk_format: 'qcow2',
  37. properties: {
  38. os_type: 'linux',
  39. },
  40. },
  41. ]
  42. const idcStandardImageParams = {
  43. limit: 0,
  44. details: true,
  45. status: 'active',
  46. is_guest_image: false,
  47. scope: 'system',
  48. 'filter.0': 'disk_format.notequals(iso)',
  49. is_standard: true,
  50. }
  51. const $route = {
  52. query: {},
  53. }
  54. let store = null
  55. let setFieldsValue = null
  56. let getFieldsValue = null
  57. let decorator = null
  58. let localVue = null
  59. beforeEach(() => {
  60. setFieldsValue = jest.fn()
  61. decorator = {
  62. os: [
  63. 'os',
  64. {
  65. initialValue: '',
  66. rules: [{ required: true, message: i18n.t('compute.text_153') }],
  67. },
  68. ],
  69. image: [
  70. 'image',
  71. {
  72. initialValue: { key: '', label: '' },
  73. },
  74. ],
  75. }
  76. store = new Vuex.Store({
  77. getters: {
  78. scope: () => 'system', // !!! 如果写成 scope: 'system' 会报错,注意
  79. userInfo () {
  80. return {
  81. projectId: 'test',
  82. }
  83. },
  84. },
  85. })
  86. localVue = createLocalVue()
  87. localVue.use(Vuex)
  88. })
  89. describe('OsSelect', () => {
  90. it('IDC standard', () => {
  91. getFieldsValue = jest.fn(() => {
  92. return {
  93. os: 'Windows',
  94. image: {
  95. key: '90b59d90-45af-4dad-86ff-c2a19f689834',
  96. label: 'windows-server-2012-R2-VL-202008',
  97. },
  98. }
  99. })
  100. const wrapper = mount(ImageSelect, {
  101. provide () {
  102. return {
  103. form: {
  104. fc: {
  105. setFieldsValue,
  106. getFieldsValue,
  107. },
  108. },
  109. }
  110. },
  111. propsData: {
  112. decorator,
  113. type: 'idc',
  114. hypervisor: 'kvm',
  115. cloudType: 'idc',
  116. imageType: IMAGES_TYPE_MAP.standard.key,
  117. form: {
  118. fc: {
  119. setFieldsValue,
  120. getFieldsValue,
  121. },
  122. },
  123. },
  124. mocks: {
  125. $route,
  126. },
  127. localVue,
  128. store,
  129. })
  130. expect(wrapper.vm.$el).toMatchSnapshot()
  131. // 测试公共镜像的参数
  132. const imagesM = Manager.mock.instances[0]
  133. expect(imagesM.list.mock.calls[0][0].params).toEqual(idcStandardImageParams)
  134. wrapper.setData({
  135. images: {
  136. list: idcStandardImageList, // 公共镜像、iso、自定义镜像 list
  137. cacheimagesList: [], // idc: 镜像缓存list,用于对比哪些镜像已缓存,public|private: image-list
  138. hostimagesList: [], // 主机镜像 list
  139. instanceSnapshotsList: [], // 主机快照 list
  140. },
  141. })
  142. // 测试组件的 osOpts 和 imageOptsMap
  143. wrapper.vm.getImagesInfo()
  144. expect(wrapper.vm.imagesInfo.osOpts).toEqual([
  145. { label: 'Windows', key: 'Windows' },
  146. { label: 'CentOS', key: 'CentOS' },
  147. { label: 'Linux', key: 'Linux' },
  148. ])
  149. expect(wrapper.vm.imagesInfo.imageOptsMap).toEqual({
  150. Windows: [
  151. {
  152. name: 'test1',
  153. id: 1,
  154. disk_format: 'qcow2',
  155. properties: {
  156. arch: 'x86_64',
  157. distro: 'Windows Server 2012 R2 Datacenter',
  158. os_type: 'Windows',
  159. partition_type: 'mbr',
  160. version: '6.3',
  161. },
  162. feData: {
  163. name: 'test1',
  164. imageType: 'Windows',
  165. },
  166. },
  167. ],
  168. CentOS: [
  169. {
  170. name: 'test2',
  171. id: 2,
  172. disk_format: 'qcow2',
  173. properties: {
  174. net_driver: '',
  175. os_distribution: 'CentOS',
  176. os_type: 'Linux',
  177. partition_type: 'mbr',
  178. uefi_support: 'true',
  179. version: '8.0.1905',
  180. },
  181. feData: { name: 'CentOS (test2)', imageType: 'CentOS' },
  182. },
  183. ],
  184. Linux: [
  185. {
  186. name: 'test31',
  187. id: 3,
  188. disk_format: 'qcow2',
  189. properties: { os_type: 'linux' },
  190. feData: { name: 'test31', imageType: 'linux' },
  191. },
  192. ],
  193. })
  194. })
  195. })