0

我在这里做 redux-orm 的教程,我需要map在我的测试中调用一个 QuerySet 实例。

回购中的原始测试在这里

这就是我创建的方式Todo

const todoTags = 'testing,nice,cool'
const user = session.User.first()
const userId = user.getId()

const action = {
  type: CREATE_TODO,
  payload: {
    text: todoText,
    tags: todoTags,
    user: userId
  }
}

const { Todo, Tag, User } = applyActionAndGetNextSession(schema, state, action)

我的代码如下所示:

const newTodo = Todo.last()
console.log(newTodo.tags.forEach, newTodo.tags.map)
console.log('Print all tags')
newTodo.tags.forEach(tag => {
  console.log('Prints a tag')
  console.log(tag)
})

newTodo.tags.map(tag => {
  console.log('Prints a tag 2')
  console.log(tag)
  return tag
})

expect(newTodo.text).toEqual(todoText)
expect(newTodo.user.getId()).toEqual(userId)
expect(newTodo.done).toBeFalsy()
const newTodoTags = newTodo.tags.map(tag => tag.name)
console.log(newTodoTags)
expect(newTodoTags).toEqual(['testing','nice','cool'])

Tag模型看起来像:

Tag.backend = {
  idAttribute: 'name'
}

我可以检索名称,这些名称恰好ids适用于该模型,使用

newTodo.tags.idArr

这是hacky和不可接受的。

测试失败,这是我的控制台输出

console.log(newTodo.tags)

 //OUTPUT
 QuerySet {
   ...
   idArr: ['testing', 'nice', 'cool']
   ...
 }

console.log(newTodo.tags.forEach, newTodo.tags.map)

//OUTPUT
[ Function forEach] [Function map]

console.log(newTodo.tags.toRefArray())

//OUTPUT
[undefined, undefined, undefined]

console.log('Print all tags')
newTodo.tags.forEach(tag => {
  console.log('Prints a tag')
  console.log(tag)
})

newTodo.tags.map(tag => {
  console.log('Prints a tag 2')
  console.log(tag)
  return tag
})

//OUTPUT
Prints all tags

console.log(newTodo.tags.withModels)

//Output is a QuerySet

newTodo.tags.withModels.map(tag => {
  console.log('mapping over tag models')
  console.log(tag)
  return tag
}

回应@markerikson 评论:

case CREATE_TODO:
    const tags = payload.tags.split(',')
    const trimmed = tags.map(tag => tag.trim())
    trimmed.forEach(tag => Tag.create(tag))
    break

Tag模型中处理减速器内的字符串。TodoTag的代码在这里

4

1 回答 1

2

正如我在评论中建议的那样:您没有正确创建 Tag 实例。看起来您正在将每个单独的标签字符串直接传递给Tag.create(),所以它就像Tag.create("testing"). 相反,您需要传递一个对象,例如Tag.create({name : "testing"}).

于 2017-01-07T00:54:46.223 回答