/* global MockSocket, MockRunner, createQUnitStartFn, createQUnitConfig reporter:true */

// Tests for adapter/qunit.src.js
// These tests are executed in browser.

describe('adapter qunit', function () {
  var Karma = window.__karma__.constructor

  describe('config', function () {
    var config
    var tc

    beforeEach(function () {
      tc = new Karma(new MockSocket(), null, null, null, {search: ''})
    })

    it('should return the default configuration passed', function () {
      tc.config.qunit = {}
      config = createQUnitConfig(tc, {autostart: false})
      expect(config.autostart).toBe(false)
    })

    it('should return the configuration defined on the runner', function () {
      tc.config.qunit = {
        autostart: false
      }
      config = createQUnitConfig(tc)
      expect(config.autostart).toBe(false)
    })

    it('should prefer configuration on the runner', function () {
      tc.config.qunit = {
        autostart: true
      }
      config = createQUnitConfig(tc, {autostart: false})
      expect(config.autostart).toBe(true)
    })

    it('should return the default config for no client config', function () {
      config = createQUnitConfig(tc, {autostart: false})
      expect(config).toEqual({autostart: false})
    })
  })

  describe('reporter', function () {
    var runner
    var tc

    beforeEach(function () {
      tc = new Karma(new MockSocket(), null, null, null, {search: ''})
      runner = new MockRunner()
      reporter = new (createQUnitStartFn(tc, runner))()
    })

    describe('done', function () {
      it('should report complete', function () {
        spyOn(tc, 'complete')

        runner.emit('done')
        expect(tc.complete).toHaveBeenCalled()
      })
    })

    describe('total number of tests', function () {
      it('should use the tracking in qunit if available', function () {
        spyOn(tc, 'info').and.callFake(function (result) {
          expect(result.total).toBe(1)
        })

        var mockQUnitResult = {
          totalTests: 1
        }

        runner.emit('begin', mockQUnitResult)
        expect(tc.info).toHaveBeenCalled()
      })

      it('should use our own tracking if none is available', function () {
        spyOn(tc, 'info').and.callFake(function (result) {
          expect(result.total).toBe(1)
        })

        var mockQUnitResult = {
          name: 'should do something',
          module: 'desc1',
          failed: 0
        }

        runner.emit('testStart', mockQUnitResult)
        runner.emit('testDone', mockQUnitResult)
        runner.emit('done')

        expect(tc.info).toHaveBeenCalled()
      })
    })

    describe('test start', function () {
      it('should create a qunit-fixture element, and remove if exists', function () {
        runner.emit('testStart', {})

        var fixture = document.getElementById('qunit-fixture')
        expect(fixture).toBeDefined()

        fixture.className = 'marker'
        runner.emit('testStart', {})

        fixture = document.getElementById('qunit-fixture')
        expect(fixture).toBeDefined()
        expect(fixture.className).not.toBe('marker')
      })
    })

    describe('test end', function () {
      it('should report result', function () {
        spyOn(tc, 'result').and.callFake(function (result) {
          expect(result.description).toBe('should do something')
          expect(result.suite instanceof Array).toBe(true)
          expect(result.success).toBe(true)
          expect(result.log instanceof Array).toBe(true)
        })

        var mockQUnitResult = {
          name: 'should do something',
          module: 'desc1',
          failed: 0
        }

        runner.emit('testStart', mockQUnitResult)
        runner.emit('testDone', mockQUnitResult)

        expect(tc.result).toHaveBeenCalled()
      })

      it('should report failed result', function () {
        spyOn(tc, 'result').and.callFake(function (result) {
          expect(result.success).toBe(false)
          expect(result.log).toEqual(['Big trouble.\nExpected: ' + JSON.stringify(expected) + '\nActual: undefined' + '\n'])
        })

        var mockQUnitResult = {
          module: 'desc1',
          failed: 1,
          name: 'should do something'
        }

        var expected = {foo: 'bar', baz: [1, 2, 3]}

        var mockQUnitLog = {
          result: false,
          message: 'Big trouble.',
          expected: expected
        }

        runner.emit('testStart', mockQUnitResult)
        runner.emit('log', mockQUnitLog)
        runner.emit('testDone', mockQUnitResult)

        expect(tc.result).toHaveBeenCalled()
      })
    })
  })
})

@@ -475,32 +475,53 @@
 , function () %7B%0A
+      tc.config = %7B%7D%0A
       tc.config.
@@ -709,32 +709,53 @@
 , function () %7B%0A
+      tc.config = %7B%7D%0A
       tc.config.
@@ -943,32 +943,53 @@
 , function () %7B%0A
+      tc.config = %7B%7D%0A
       tc.config.
@@ -1707,16 +1707,42 @@
 mplete')
+%0A        spyOn(tc, 'info')
 %0A%0A      
@@ -1759,24 +1759,67 @@
 mit('done')%0A
+        expect(tc.info).toHaveBeenCalled()%0A
         expe
@@ -2356,36 +2356,38 @@
      spyOn(tc, '
-info
+result
 ').and.callFake(
@@ -2434,32 +2434,115 @@
 ult.
-total).toBe(1)%0A        %7D
+description).toBe('should do something')%0A        %7D)%0A%0A        spyOn(tc, 'info')%0A        spyOn(tc, 'complete'
 )%0A%0A 
@@ -2807,36 +2807,128 @@
       expect(tc.
-info
+result).toHaveBeenCalled()%0A        expect(tc.info).toHaveBeenCalled()%0A        expect(tc.complete
 ).toHaveBeenCall

add namespaces and spies unit tests fail because of missing namespaces