Skip to content

Commit

Permalink
fix: tests for js-ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Sep 18, 2019
1 parent 2d3b8c7 commit 17e1a5b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
13 changes: 7 additions & 6 deletions src/bin/case.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { exec } from './exec'
import { ComponentParser } from '../transpilers/component-parser'
import { readdirSync, writeFileSync, existsSync } from 'fs'
import { readFileSync, readdirSync, writeFileSync, existsSync } from 'fs'
import { resolve, join } from 'path'
import { compileToSource as compileToJSSource } from '../js-ssr'
import { compileToSource as compileToPHPSource } from '../php-ssr'
Expand All @@ -13,6 +13,7 @@ const tsconfigPath = resolve(__dirname, '../../test/tsconfig.json')
const cases = readdirSync(caseRoot)

export function compileToJS (caseName) {
// if (caseName !== 'nest-for-computed') return
const caseDir = join(caseRoot, caseName)
const ts = join(caseDir, 'component.ts')
let componentClass
Expand All @@ -22,8 +23,8 @@ export function compileToJS (caseName) {
const ccj = new ToJSCompiler(tsconfigPath)
componentClass = ccj.compileAndRun(component.get(ts))['default']
} else {
const js = join(caseDir, 'component.js')
componentClass = existsSync(ts) ? require(ts).default : require(js)
const js = resolve(caseDir, 'component.js')
componentClass = require(js)
}

const fn = compileToJSSource(componentClass)
Expand All @@ -37,6 +38,7 @@ export function compileAllToJS () {
export function compileToPHP (caseName) {
const caseDir = join(caseRoot, caseName)
const ts = join(caseDir, 'component.ts')
const ccj = new ToJSCompiler(tsconfigPath)
let componentClass
let code = ''

Expand All @@ -46,12 +48,11 @@ export function compileToPHP (caseName) {
nsPrefix: 'san\\components\\test\\'
})
const component = new ComponentParser(ts, tsconfigPath).parseComponent()
const ccj = new ToJSCompiler(tsconfigPath)
componentClass = ccj.compileAndRun(component.get(ts))['default']
code += ccp.compileComponent(component)
} else {
const js = join(caseDir, 'component.js')
componentClass = require(js)
const js = resolve(caseDir, 'component.js')
componentClass = ccj.run(readFileSync(js, 'utf8'))
}

const renderCode = compileToPHPSource(
Expand Down
2 changes: 1 addition & 1 deletion src/js-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6641,7 +6641,7 @@ function genComponentProtoCode (component) {

computedCode.push(key + ': ' +
computed.toString()
.replace(/^\s*function\s*\(/, 'function (componentCtx')
.replace(/^\s*function\s*(\S+)?\(/, 'function $1 (componentCtx')
.replace(
/this.data.get\(([^)]+)\)/g,
function (match, exprLiteral) {
Expand Down
1 change: 1 addition & 0 deletions src/php-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6621,6 +6621,7 @@ function genComponentProtoCode (component) {
export function compileToSource (ComponentClass, { funcName = '' } = {}) {
guid = 1
ssrIndex = 0

const sourceBuffer = new CompileSourceBuffer()
const contextId = genSSRId()

Expand Down
4 changes: 4 additions & 0 deletions src/transpilers/ts2js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export class Compiler {

compileAndRun (source: SanSourceFile) {
const js = this.compileToJS(source)
return this.run(js)
}

run (js: string) {
const fn = new Function('module', 'exports', 'require', js) // eslint-disable-line
const module = {
exports: {}
Expand Down
19 changes: 12 additions & 7 deletions test/integration.spec.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
const { readFileSync, readdirSync } = require('fs')
const { resolve, join } = require('path')
const { renderByJS, renderByPHP, compileAllToPHP, compileAllToJS } = require('../dist/bin/case')
const { renderByJS, renderByPHP, compileToPHP, compileToJS } = require('../dist/bin/case')

const caseRoot = resolve(__dirname, 'cases')
const files = readdirSync(caseRoot)

// compileAllToJS()
compileAllToPHP()

for (const caseName of files) {
// if (caseName !== 'nest-for-computed') continue

const caseDir = resolve(caseRoot, caseName)
const htmlPath = join(caseDir, 'result.html')
const phpPath = join(caseDir, 'ssr.php')
const expected = readFileSync(htmlPath, 'utf8')

it(caseName, function () {
// expect(renderByJS(caseName)).toBe(expected)
it('js:' + caseName, function () {
compileToJS(caseName)

expect(renderByJS(caseName)).toBe(expected)
})

it('php:' + caseName, function () {
compileToPHP(caseName)

expect(renderByPHP(caseName)).toBe(expected)
})
}

0 comments on commit 17e1a5b

Please sign in to comment.