components.spec.js 5.9 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
const parser = require('@babel/parser')

const scopedComponentTraverse = require('../lib/babel/scoped-component-traverse')
const globalComponentTraverse = require('../lib/babel/global-component-traverse')

process.UNI_LIBRARIES = ['@dcloudio/uni-ui']

function assertCodegen (content, expectedComponents, isScoped = true) {
  const {
    state: {
      components
    }
  } = (isScoped ? scopedComponentTraverse : globalComponentTraverse)(parser.parse(content, {
    sourceType: 'module',
    plugins: [
      'typescript'
    ]
  }), {
    components: []
  })

  expect(JSON.stringify(components)).toBe(JSON.stringify(expectedComponents))
}

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
function assertCodegenOptions (content, expectedOptions, isScoped = true) {
  const {
    state: {
      options
    }
  } = (isScoped ? scopedComponentTraverse : globalComponentTraverse)(parser.parse(content, {
    sourceType: 'module',
    plugins: [
      'typescript'
    ]
  }), {
    components: [],
    options: {
      name: null,
      inheritAttrs: null,
      props: null
    }
  })

  expect(JSON.stringify(options)).toBe(JSON.stringify(expectedOptions))
}

fxy060608's avatar
fxy060608 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60
describe('mp:loader', () => {
  it('parse scoped component', () => {
    assertCodegen(
      `
import mediaList from '@/components/tab-nvue/mediaList.vue';
import uniLoadMore from '@/components/uni-load-more.vue';
export default {
    components: {
        mediaList,
        uniLoadMore
    }
}
`,
      [{
fxy060608's avatar
fxy060608 已提交
61 62 63
        name: 'mediaList',
        value: 'mediaList',
        source: '@/components/tab-nvue/mediaList.vue'
fxy060608's avatar
fxy060608 已提交
64
      }, {
fxy060608's avatar
fxy060608 已提交
65 66 67
        name: 'uniLoadMore',
        value: 'uniLoadMore',
        source: '@/components/uni-load-more.vue'
fxy060608's avatar
fxy060608 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80
      }])

    assertCodegen(
      `
            import { uniBadge,uniCard} from '@dcloudio/uni-ui';
            export default {
                components: {
                    'uni-badge':uniBadge,
                    'uni-card':uniCard
                }
            }
            `,
      [{
fxy060608's avatar
fxy060608 已提交
81 82 83
        name: 'uni-badge',
        value: 'uniBadge',
        source: '@dcloudio/uni-ui/lib/uni-badge/uni-badge'
fxy060608's avatar
fxy060608 已提交
84
      }, {
fxy060608's avatar
fxy060608 已提交
85 86 87
        name: 'uni-card',
        value: 'uniCard',
        source: '@dcloudio/uni-ui/lib/uni-card/uni-card'
fxy060608's avatar
fxy060608 已提交
88
      }])
fxy060608's avatar
fxy060608 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

    assertCodegen(
      `
import VanIcon from '../icon/index.vue'
import VanPopup from '../icon/popup.vue'
import VanLoading from '../icon/loading.vue'
global['__wxVueOptions'] = {
  components:{
    'van-icon': VanIcon,
    'van-popup': VanPopup,
    'van-loading': VanLoading
  }
}
                `,
      [{
        name: 'van-icon',
        value: 'VanIcon',
        source: '../icon/index.vue'
      },
      {
        name: 'van-popup',
fxy060608's avatar
fxy060608 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
        value: 'VanPopup',
        source: '../icon/popup.vue'
      },
      {
        name: 'van-loading',
        value: 'VanLoading',
        source: '../icon/loading.vue'
      }
      ])

    assertCodegen(
      `
      import VanIcon from '../icon/index.vue'
      import VanPopup from '../icon/popup.vue'
      import VanLoading from '../icon/loading.vue'
      exports.default.components = Object.assign({
          'van-icon': VanIcon,
          'van-popup': VanPopup,
          'van-loading': VanLoading
        },exports.default.components || {})
                      `,
      [{
        name: 'van-icon',
        value: 'VanIcon',
        source: '../icon/index.vue'
      },
      {
        name: 'van-popup',
fxy060608's avatar
fxy060608 已提交
138 139 140 141 142 143 144 145 146
        value: 'VanPopup',
        source: '../icon/popup.vue'
      },
      {
        name: 'van-loading',
        value: 'VanLoading',
        source: '../icon/loading.vue'
      }
      ])
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

    assertCodegen(
      `import myButton from '@/components/my-button/my-button.vue';
      export default {
          components: {
            myButton
          }
      }
      import VanButton from '../button/index.vue'
      import VanSearch from '../search/index.vue'
      exports.default.components = Object.assign({
          'van-button': VanButton,
          'van-search': VanSearch,
        },exports.default.components || {})`,
      [
        {
          name: 'van-button',
          value: 'VanButton',
          source: '../button/index.vue'
        },
        {
          name: 'van-search',
          value: 'VanSearch',
          source: '../search/index.vue'
        },
        {
          name: 'myButton',
          value: 'myButton',
          source: '@/components/my-button/my-button.vue'
        }
      ])
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

    assertCodegenOptions(
      `export default {
        name: 'test'
      }`,
      {
        name: '"test"',
        inheritAttrs: null,
        props: null
      }
    )

    assertCodegenOptions(
      `export default {
        props: ['id', 'test']
      }`,
      {
        name: null,
        inheritAttrs: null,
        props: '["id","test"]'
      }
    )

    assertCodegenOptions(
      `export default {
        props: {
          id: {
            type: String
          },
          'test': {
            type: String
          }
        }
      }`,
      {
        name: null,
        inheritAttrs: null,
        props: '["id","test"]'
      }
    )
fxy060608's avatar
fxy060608 已提交
218 219 220 221 222 223 224 225 226 227 228 229
  })

  it('parse global component', () => {
    assertCodegen(
      `
            import { uniBadge,uniCard} from '@dcloudio/uni-ui';
            import mediaList from '@/components/tab-nvue/mediaList.vue';
            Vue.component('uni-badge',uniBadge)
            Vue.component('uni-card',uniCard)
            Vue.component('media-list',mediaList)
            `,
      [{
fxy060608's avatar
fxy060608 已提交
230 231 232
        name: 'uni-badge',
        value: 'uniBadge',
        source: '@dcloudio/uni-ui/lib/uni-badge/uni-badge'
fxy060608's avatar
fxy060608 已提交
233
      }, {
fxy060608's avatar
fxy060608 已提交
234 235 236
        name: 'uni-card',
        value: 'uniCard',
        source: '@dcloudio/uni-ui/lib/uni-card/uni-card'
fxy060608's avatar
fxy060608 已提交
237
      }, {
fxy060608's avatar
fxy060608 已提交
238 239 240
        name: 'media-list',
        value: 'mediaList',
        source: '@/components/tab-nvue/mediaList.vue'
fxy060608's avatar
fxy060608 已提交
241 242
      }], false)
  })
243
})