json.spec.ts 5.5 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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
import { I18N_JSON_DELIMITERS } from '@dcloudio/uni-shared'
import { parseI18nJson, compileI18nJsonStr } from '../src/json'
const delimiters: [string, string] = I18N_JSON_DELIMITERS
describe('parseI18nJson', () => {
  test('pages.json->style', () => {
    const pageMeta = parseI18nJson(
      {
        tabBarIndex: 0,
        bounce: 'vertical',
        navigationBar: {
          titleText: '%component.title%组件',
          buttons: [
            {
              text: '%component.btns.0.text%',
              fontSrc: '/static/uni.ttf',
              fontSize: '22px',
              color: '#FFFFFF',
            },
          ],
        },
        route: 'pages/tabBar/component/component',
      },
      {
        'component.title': '内置',
        'component.btns.0.text': '\ue534',
      },
      delimiters
    ) as Record<string, any>
    expect(pageMeta.navigationBar.titleText).toBe('内置组件')
    expect(pageMeta.navigationBar.buttons[0].text).toBe('\ue534')
  })
})
describe('compileI18nJsonStr', () => {
  test('empty', () => {
    expect(
      compileI18nJsonStr(JSON.stringify({}), {
        locale: '',
        locales: {},
        delimiters,
      })
    ).toBe('{}')
  })
  test('pages.json->tabBar', () => {
fxy060608's avatar
fxy060608 已提交
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
    expect(
      compileI18nJsonStr(
        JSON.stringify({
          color: '#7A7E83',
          selectedColor: '#007AFF',
          borderStyle: 'black',
          backgroundColor: '#%tabBar.backgroundColor%',
          list: [
            {
              pagePath: 'pages/tabBar/component/component',
              iconPath: 'static/component.png',
              selectedIconPath: 'static/componentHL.png',
              text: '%tabBar.0.title%',
            },
            {
              pagePath: 'pages/tabBar/API/API',
              iconPath: 'static/api.png',
              selectedIconPath: 'static/apiHL.png',
              text: '%tabBar.1.title%',
            },
          ],
        }),
        {
          locale: 'zh-Hans',
          locales: {
            'zh-Hans': {
              'tabBar.backgroundColor': 'f8f8f8',
              'tabBar.0.title': '组件',
              'tabBar.1.title': '接口',
            },
          },
          delimiters,
        }
      )
    ).toMatchSnapshot()
  })
  test('pages.json->tabBar with multi language', () => {
fxy060608's avatar
fxy060608 已提交
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
    expect(
      compileI18nJsonStr(
        JSON.stringify({
          color: '#7A7E83',
          selectedColor: '#007AFF',
          borderStyle: 'black',
          backgroundColor: '#%tabBar.backgroundColor%',
          list: [
            {
              pagePath: 'pages/tabBar/component/component',
              iconPath: 'static/component.png',
              selectedIconPath: 'static/componentHL.png',
              text: '%tabBar.0.title%',
            },
            {
              pagePath: 'pages/tabBar/API/API',
              iconPath: 'static/api.png',
              selectedIconPath: 'static/apiHL.png',
              text: '%tabBar.1.title%',
            },
          ],
        }),
        {
          locale: 'zh-Hans',
          locales: {
            'zh-Hans': {
              'tabBar.backgroundColor': 'f8f8f8',
              'tabBar.0.title': '组件',
              'tabBar.1.title': '接口',
            },
            en: {
              'tabBar.backgroundColor': 'f6f6f6',
              'tabBar.0.title': 'Component',
              'tabBar.1.title': 'API',
            },
          },
          delimiters,
        }
      )
    ).toMatchSnapshot()
  })
fxy060608's avatar
fxy060608 已提交
122

fxy060608's avatar
fxy060608 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 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 178 179 180
  test('androidPrivacy.json', () => {
    expect(
      compileI18nJsonStr(
        JSON.stringify({
          version: '1',
          prompt: 'template',
          title: '%p.title%',
          message: '  %p.message%',
          buttonAccept: '%p.accept%',
          buttonRefuse: '%p.refuse%',
          second: {
            title: '%p.second.title%',
            message:
              '  进入应用前,你需先同意<a href="">《服务协议》</a>和<a href="">《隐私政策》</a>,否则将退出应用。',
            buttonAccept: '同意并继续',
            buttonRefuse: '退出应用',
          },
          styles: {
            backgroundColor: '#00FF00',
            borderRadius: '5px',
            title: {
              color: '%p.title.color%',
            },
            buttonAccept: {
              color: '#ffff00',
            },
            buttonRefuse: {
              color: '#00ffff',
            },
          },
        }),
        {
          locale: 'zh-Hans',
          locales: {
            'zh-Hans': {
              'p.title': '服务协议和隐私政策',
              'p.message':
                '请你务必审慎阅读、充分理解“服务协议”和“隐私政策”各条款,包括但不限于:为了更好的向你提供服务,我们需要收集你的设备标识、操作日志等信息用于分析、优化应用性能。<br/>  你可阅读<a href="">《服务协议》</a>和<a href="">《隐私政策》</a>了解详细信息。如果你同意,请点击下面按钮开始接受我们的服务。',
              'p.accept': '同意并接受',
              'p.refuse': '暂不同意',
              'p.second.title': '确认提示',
              'p.title.color': '#ff00ff',
            },
            en: {
              'p.title': 'Privacy',
              'p.message': 'privacy',
              'p.accept': 'accept',
              'p.refuse': 'refuse',
              'p.second.title': 'confirm',
              'p.title.color': '#ff00ff',
            },
          },
          delimiters,
        }
      )
    ).toMatchSnapshot()
  })
})