<commit_before>_               = require 'underscore'
rewire          = require 'rewire'
benv            = require 'benv'
Backbone        = require 'backbone'
sinon           = require 'sinon'
{ resolve }     = require 'path'
{ fabricate }   = require 'antigravity'

Artist        = require '../../../../models/artist'
Artwork       = require '../../../../models/artwork'
CurrentUser   = require '../../../../models/current_user'

describe 'ArtworkView', ->
  before (done) ->
    benv.setup =>
      benv.expose { $: benv.require 'jquery' }
      Backbone.$ = $
      $.support.transition = { end: 'transitionend' }
      $.fn.emulateTransitionEnd = -> @trigger $.support.transition.end
      done()

  after ->
    benv.teardown()

  beforeEach (done) ->
    @artist = new Artist(fabricate 'artist')
    sinon.stub @artist.artworks, 'fetch'

    # Interestingly: jQuery "Every attempt is made to convert the string to a
    # JavaScript value (this includes booleans, numbers, objects, arrays, and null)"
    # and we're using numeric-looking strings for fabricated artwork image IDs.
    edition_sets = _.times(2, -> fabricate('edition_set', forsale: true, acquireable: true))
    artwork = fabricate('artwork', edition_sets: edition_sets, acquireable: true)
    _.each (artwork).images, (image) ->
      image.id = _.uniqueId('stringy')
    @artwork = new Artwork(artwork, parse: true)

    sinon.stub Backbone, 'sync'
    benv.render resolve(__dirname, '../../templates/index.jade'), {
      sd: {}
      artist: @artist
      artwork: @artwork
    }, =>
      @ArtworkView = benv.requireWithJadeify(
        (resolve __dirname, '../../client/view'),
        ['artistArtworksTemplate', 'detailTemplate']
      )
      @ArtworkView.__set__ 'ShareView', (@shareViewStub = sinon.stub())
      @ArtworkView.__set__ 'acquireArtwork', (@acquireArtworkStub = sinon.stub())
      @renderDetailSpy = sinon.spy @ArtworkView::, 'renderDetail'
      done()

  afterEach ->
    Backbone.sync.restore()
    @artist.artworks.fetch.restore()
    @renderDetailSpy.restore()

  describe 'user logged in', ->
    beforeEach ->
      @ArtworkView.__set__ 'CurrentUser', { orNull: -> new CurrentUser(fabricate 'user') }
      @view = new @ArtworkView el: $('#artwork-page'), artist: @artist, artwork: @artwork

    describe 'when an artwork changes', ->
      it 'updates the detail panel accordingly', ->
        $detail = @view.$('.artwork-detail')
        $detail.find('.artwork-buy-button').length.should.be.ok
        @view.artwork.set 'sale_message', 'SOLD'
        $detail.find('.artwork-buy-button').length.should.not.be.ok

      it 'only renders if the artwork changes', ->
        @renderDetailSpy.called.should.not.be.ok
        @view.artwork.set 'sale_message', 'SOLD'
        @renderDetailSpy.called.should.be.ok

    describe '#initialize', -><commit_msg>Remove irrelevant spec<commit_after>_               = require 'underscore'
rewire          = require 'rewire'
benv            = require 'benv'
Backbone        = require 'backbone'
sinon           = require 'sinon'
{ resolve }     = require 'path'
{ fabricate }   = require 'antigravity'

Artist        = require '../../../../models/artist'
Artwork       = require '../../../../models/artwork'
CurrentUser   = require '../../../../models/current_user'

describe 'ArtworkView', ->
  before (done) ->
    benv.setup =>
      benv.expose { $: benv.require 'jquery' }
      Backbone.$ = $
      $.support.transition = { end: 'transitionend' }
      $.fn.emulateTransitionEnd = -> @trigger $.support.transition.end
      done()

  after ->
    benv.teardown()

  beforeEach (done) ->
    @artist = new Artist(fabricate 'artist')
    sinon.stub @artist.artworks, 'fetch'

    # Interestingly: jQuery "Every attempt is made to convert the string to a
    # JavaScript value (this includes booleans, numbers, objects, arrays, and null)"
    # and we're using numeric-looking strings for fabricated artwork image IDs.
    edition_sets = _.times(2, -> fabricate('edition_set', forsale: true, acquireable: true))
    artwork = fabricate('artwork', edition_sets: edition_sets, acquireable: true)
    _.each (artwork).images, (image) ->
      image.id = _.uniqueId('stringy')
    @artwork = new Artwork(artwork, parse: true)

    sinon.stub Backbone, 'sync'
    benv.render resolve(__dirname, '../../templates/index.jade'), {
      sd: {}
      artist: @artist
      artwork: @artwork
    }, =>
      @ArtworkView = benv.requireWithJadeify(
        (resolve __dirname, '../../client/view'),
        ['artistArtworksTemplate', 'detailTemplate']
      )
      @ArtworkView.__set__ 'ShareView', (@shareViewStub = sinon.stub())
      @ArtworkView.__set__ 'acquireArtwork', (@acquireArtworkStub = sinon.stub())
      @renderDetailSpy = sinon.spy @ArtworkView::, 'renderDetail'
      done()

  afterEach ->
    Backbone.sync.restore()
    @artist.artworks.fetch.restore()
    @renderDetailSpy.restore()

  describe 'user logged in', ->
    beforeEach ->
      @ArtworkView.__set__ 'CurrentUser', { orNull: -> new CurrentUser(fabricate 'user') }
      @view = new @ArtworkView el: $('#artwork-page'), artist: @artist, artwork: @artwork

    describe 'when an artwork changes', ->
      it 'only renders if the artwork changes', ->
        @renderDetailSpy.called.should.not.be.ok
        @view.artwork.set 'sale_message', 'SOLD'
        @renderDetailSpy.called.should.be.ok

    describe '#initialize', ->
{'fraction_non_alphanumeric': 0.15060684075027583, 'fraction_numerical': 0.0003677822728944465, 'mean_word_length': 2.782336578581363, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4106362', 'n_tokens_mistral': 1679, 'n_tokens_neox': 1666, 'n_words': 387}