Example #1
0
  it("can have all_models with cycles through lists", () => {
    const d = new Document()
    expect(d.roots().length).to.equal(0)
    expect(size(d._all_models)).to.equal(0)

    const root1 = new SomeModelWithChildren()
    const root2 = new SomeModelWithChildren()
    const child1 = new SomeModelWithChildren()
    root1.children = [child1]
    root2.children = [child1]
    child1.children = [root1]
    d.add_root(root1)
    d.add_root(root2)
    expect(d.roots().length).to.equal(2)
    expect(size(d._all_models)).to.equal(3)

    root1.children = []
    expect(size(d._all_models)).to.equal(3)

    root2.children = []
    expect(size(d._all_models)).to.equal(2)

    root1.children = [child1]
    expect(size(d._all_models)).to.equal(3)
  })
Example #2
0
  it("sets proper document on models added during construction", () => {
    const d = new Document()
    expect(d.roots().length).to.equal(0)
    expect(Object.keys(d._all_models).length).to.equal(0)

    const root1 = new ModelWithConstructTimeChanges()
    // change it so it doesn't match what initialize() does
    root1.foo = 3
    root1.child = null
    d.add_root(root1)

    const json = d.to_json_string()
    const parsed = JSON.parse(json)
    parsed.version = js_version
    const copy = Document.from_json_string(JSON.stringify(parsed))

    const root1_copy = copy.get_model_by_id(root1.id) as ModelWithConstructTimeChanges

    expect(root1.foo).to.equal(3)
    expect(root1.child).to.equal(null)

    // when unpacking the copy, initialize() was supposed to overwrite
    // what we unpacked.
    expect(root1_copy.foo).to.equal(4)
    expect(root1_copy.child).to.be.an.instanceof(AnotherModel)
    expect(root1_copy.document).to.equal(copy)
    expect(root1_copy.child!.document).to.equal(copy)
  })
Example #3
0
  it("can have all_models with cycles", () => {
    const d = new Document()
    expect(d.roots().length).to.equal(0)
    expect(size(d._all_models)).to.equal(0)

    const root1 = new SomeModel()
    const root2 = new SomeModel()
    const child1 = new SomeModel()
    root1.child = child1
    root2.child = child1
    child1.child = root1
    d.add_root(root1)
    d.add_root(root2)
    expect(d.roots().length).to.equal(2)
    expect(size(d._all_models)).to.equal(3)

    root1.child = null
    expect(size(d._all_models)).to.equal(3)

    root2.child = null
    expect(size(d._all_models)).to.equal(2)

    root1.child = child1
    expect(size(d._all_models)).to.equal(3)
  })
  beforeEach(() => {
    const doc = new Document()

    const plot = new Plot({
       x_range: new Range1d({start: 0, end: 1}),
       y_range: new Range1d({start: 0, end: 1}),
    })
    doc.add_root(plot)
    const plot_view = new plot.default_view({model: plot, parent: null}).build()

    node_source = new ColumnDataSource({
      data: {
        index: [10, 20, 30, 40],
      },
    })
    edge_source = new ColumnDataSource({
      data: {
        start: [10, 10, 30],
        end: [20, 30, 20],
      },
    })
    const node_renderer = new GlyphRenderer({data_source: node_source, glyph: new Circle()})
    const edge_renderer = new GlyphRenderer({data_source: edge_source, glyph: new MultiLine()})

    gr = new GraphRenderer({
      node_renderer,
      edge_renderer,
      layout_provider: new TrivialLayoutProvider(),
    })

    gv = new gr.default_view({model: gr, parent: plot_view}) as GraphRendererView

    node_stub = sinon.stub(gv.node_view.glyph, "hit_test")
    edge_stub = sinon.stub(gv.edge_view.glyph, "hit_test")
  })
Example #5
0
 it("lets us get_model_by_name", () => {
   const d = new Document()
   const m = new SomeModel({ name: "foo" })
   const m2 = new AnotherModel({ name: "bar" })
   m.child = m2
   d.add_root(m)
   expect(d.get_model_by_name(m.name!)).to.equal(m)
   expect(d.get_model_by_name(m2.name!)).to.equal(m2)
   expect(d.get_model_by_name("invalidid")).to.equal(null)
 })
Example #6
0
 it("lets us get_model_by_id", () => {
   const d = new Document()
   const m = new SomeModel()
   const m2 = new AnotherModel()
   m.child = m2
   d.add_root(m)
   expect(d.get_model_by_id(m.id)).to.equal(m)
   expect(d.get_model_by_id(m2.id)).to.equal(m2)
   expect(d.get_model_by_id("invalidid")).to.equal(null)
 })
Example #7
0
 function mkplot(tool: Tool): PlotView {
   const plot = new Plot({
     x_range: new Range1d({start: -1, end: 1}),
     y_range: new Range1d({start: -1, end: 1}),
   })
   plot.add_tools(tool)
   const document = new Document()
   document.add_root(plot)
   return new plot.default_view({model: plot, parent: null}).build()
 }
Example #8
0
  it("can patch an integer property", () => {
    const d = new Document()
    expect(d.roots().length).to.equal(0)
    expect(Object.keys(d._all_models).length).to.equal(0)

    const root1 = new SomeModel({ foo: 42 })
    const root2 = new SomeModel({ foo: 43 })
    const child1 = new SomeModel({ foo: 44 })
    root1.child = child1
    root2.child = child1
    d.add_root(root1)
    d.add_root(root2)
    expect(d.roots().length).to.equal(2)

    const event1 = new ev.ModelChangedEvent(d, root1, 'foo', root1.foo, 57)
    const patch1 = d.create_json_patch_string([event1])
    d.apply_json_patch(JSON.parse(patch1))

    expect(root1.foo).to.equal(57)

    const event2 = new ev.ModelChangedEvent(d, child1, 'foo', child1.foo, 67)
    const patch2 = d.create_json_patch_string([event2])
    d.apply_json_patch(JSON.parse(patch2))

    expect(child1.foo).to.equal(67)
  })
Example #9
0
 it("should round-trip through document serialization", () => {
   const d = new Document()
   d.add_root(r)
   const json = d.to_json_string()
   const parsed = JSON.parse(json)
   parsed.version = js_version
   const copy = Document.from_json_string(JSON.stringify(parsed))
   const r_copy = copy.get_model_by_id(r.id)! as CustomJSHover
   const rng_copy = copy.get_model_by_id(rng.id)! as CustomJSHover
   expect(r.values).to.be.deep.equal([rng])
   expect(r_copy.values).to.be.deep.equal([rng_copy])
 })
Example #10
0
 it("throws on get_model_by_name with duplicate name", () => {
   const d = new Document()
   const m = new SomeModel({ name: "foo" })
   const m2 = new AnotherModel({ name: "foo" })
   d.add_root(m)
   d.add_root(m2)
   let got_error = false
   try {
     d.get_model_by_name('foo')
   } catch (e) {
     got_error = true
     expect(e.message).to.include('Multiple models')
   }
   expect(got_error).to.equal(true)
 })