<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'
Fair          = require '../../../../models/fair'
Sale          = require '../../../../models/sale'
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', 'auctionPlaceholderTemplate']
      )
      @ArtworkView.__set__ 'analytics', { abTest: sinon.stub(), delta: sinon.stub(), track: { click: sinon.stub() } }
      @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') }
      @ArtworkView.__set__ 'analytics', { track: { impression: (->), click: (->) } , abTest: -> }
      @view = new @ArtworkView el: $('#artwork-page'), artist: @artist, artwork: @artwork

    describe '#checkQueryStringForAuction', ->
      it 'renders the auction placeholder when an auction_id is in the query string', ->
        @view.$el.html().should.not.include 'Bid'
        window.location.search = '?auction_id=my-sale-id'
        @view.checkQueryStringForAuction()
        html = @view.$el.html()
        html.should.include 'my-sale-id'
        html.should.include 'Bid'

    describe 'when an artwork changes', ->
      it 'only renders if the artwork changes', ->
        @renderDetailSpy.called.should.not.be.ok
        @view.artwork.set 'sale_message', 'SOLD'<commit_msg>test for if ecommerce changes on a work<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'
Fair          = require '../../../../models/fair'
Sale          = require '../../../../models/sale'
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', 'auctionPlaceholderTemplate']
      )
      @ArtworkView.__set__ 'analytics', { abTest: sinon.stub(), delta: sinon.stub(), track: { click: sinon.stub() } }
      @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') }
      @ArtworkView.__set__ 'analytics', { track: { impression: (->), click: (->) } , abTest: -> }
      @view = new @ArtworkView el: $('#artwork-page'), artist: @artist, artwork: @artwork

    describe '#checkQueryStringForAuction', ->
      it 'renders the auction placeholder when an auction_id is in the query string', ->
        @view.$el.html().should.not.include 'Bid'
        window.location.search = '?auction_id=my-sale-id'
        @view.checkQueryStringForAuction()
        html = @view.$el.html()
        html.should.include 'my-sale-id'
        html.should.include 'Bid'

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

      it 'only renders if the artwork ecommerce changes', ->
        @renderDetailSpy.called.should.not.be.ok
        @view.artwork.set 'ecommerce', true
{'fraction_non_alphanumeric': 0.15483102575554564, 'fraction_numerical': 0.0002977519726068185, 'mean_word_length': 2.7363737486095663, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 44, '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': '4106363', 'n_tokens_mistral': 2074, 'n_tokens_neox': 2063, 'n_words': 482}